Convert InputStream to JSONObject

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

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 -> BufferedReader -> StringBuilder -> loop -> JSONObject.toString().

    InputStream inputStreamObject = PositionKeeperRequestTest.class.getResourceAsStream(jsonFileName);     BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStreamObject, "UTF-8"));     StringBuilder responseStrBuilder = new StringBuilder();      String inputStr;     while ((inputStr = streamReader.readLine()) != null)         responseStrBuilder.append(inputStr);      JSONObject jsonObject = new JSONObject(responseStrBuilder.toString()); 

回答1:

Since you're already using Google's Json-Simple library, you can parse the json from an InputStream like this:

InputStream inputStream = ... //Read from a file, or a HttpRequest, or whatever. JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject)jsonParser.parse(       new InputStreamReader(inputStream, "UTF-8")); 


回答2:

If you don't want to mess with ready libraries you can just make a class like this.

public class JsonConverter {  //Your class here, or you can define it in the constructor Class requestclass = PositionKeeperRequestTest.class;  //Filename String jsonFileName;  //constructor public myJson(String jsonFileName){     this.jsonFileName = jsonFileName; }   //Returns a json object from an input stream private JSONObject getJsonObject(){      //Create input stream     InputStream inputStreamObject = getRequestclass().getResourceAsStream(jsonFileName);     try {        BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStreamObject, "UTF-8"));        StringBuilder responseStrBuilder = new StringBuilder();         String inputStr;        while ((inputStr = streamReader.readLine()) != null)            responseStrBuilder.append(inputStr);         JSONObject jsonObject = new JSONObject(responseStrBuilder.toString());         //returns the json object        return jsonObject;     } catch (IOException e) {        e.printStackTrace();    } catch (JSONException e) {        e.printStackTrace();    }      //if something went wrong, return null     return null; }  private Class getRequestclass(){     return requestclass; } } 

Then, you can use it like this:

JSONObject jObject = new JsonConverter(FILE_NAME).getJsonObject(); 


回答3:

use JsonReader in order to parse the InputStream. See example inside the API: http://developer.android.com/reference/android/util/JsonReader.html



回答4:

This code works

BufferedReader bR = new BufferedReader(  new InputStreamReader(inputStream)); String line = "";  StringBuilder responseStrBuilder = new StringBuilder(); while((line =  bR.readLine()) != null){      responseStrBuilder.append(line); } inputStream.close();  JSONObject result= new JSONObject(responseStrBuilder.toString());        


回答5:

you could use an Entity:

FileEntity entity = new FileEntity(jsonFile, "application/json"); String jsonString = EntityUtils.toString(entity) 


回答6:

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" } 


回答7:

This worked for me:

JSONArray jsonarr = (JSONArray) new JSONParser().parse(new InputStreamReader(Nameofclass.class.getResourceAsStream(pathToJSONFile))); JSONObject jsonobj = (JSONObject) new JSONParser().parse(new InputStreamReader(Nameofclass.class.getResourceAsStream(pathToJSONFile))); 


回答8:

Simple Solution:

JsonElement element = new JsonParser().parse(new InputStreamReader(inputStream)); JSONObject jsonObject = new JSONObject(element.getAsJsonObject().toString()); 


回答9:

Another solution: use flexjson.jar: http://mvnrepository.com/artifact/net.sf.flexjson/flexjson/3.2

List yourEntityList = deserializer.deserialize(new InputStreamReader(input)); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!