Convert InputStream to JSONObject

前端 未结 13 1893
刺人心
刺人心 2020-12-05 03:52

I am converting InputStream to JSONObject using following code. My question is, is there any simple way to convert InputStream to JSONObject. Without doing InputStream -> Bu

13条回答
  •  一个人的身影
    2020-12-05 04:55

    You can use this api https://code.google.com/p/google-gson/
    It's simple and very useful,

    Here's how to use the https://code.google.com/p/google-gson/ Api to resolve your problem

    public class Test {
      public static void main(String... strings) throws FileNotFoundException  {
        Reader reader = new FileReader(new File("/json.js"));
        JsonElement elem = new JsonParser().parse(reader);
        Gson gson  = new GsonBuilder().create();
       TestObject o = gson.fromJson(elem, TestObject.class);
       System.out.println(o);
      }
    
    
    }
    
    class TestObject{
      public String fName;
      public String lName;
      public String toString() {
        return fName +" "+lName;
      }
    }
    


    json.js file content :

    {"fName":"Mohamed",
    "lName":"Ali"
    }
    

提交回复
热议问题