将InputStream转换为JSONObject

我使用下面的代码将InputStream转换为JSONObject。 我的问题是,有什么简单的方法来将InputStream转换为JSONObject。 不用做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()); 

Solutions Collecting From Web of "将InputStream转换为JSONObject"

由于您已经在使用Google的Json-Simple库,因此您可以像这样从InputStreamparsingjson:

 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")); 

如果你不想捣乱现成的图书馆,你可以像这样做一个类。

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

那么,你可以像这样使用它:

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

使用JsonReader来parsingInputStream。 请参阅API中的示例: http : //developer.android.com/reference/android/util/JsonReader.html

此代码工作

 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()); 

你可以使用一个实体:

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

您可以使用此API https://code.google.com/p/google-gson/
这很简单,非常有用,

以下是如何使用https://code.google.com/p/google-gson/ Api来解​​决您的问题

 public class Test { public static void main(String... strings) throws FileNotFoundException { Reader reader = new FileReader(new File("<fullPath>/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文件内容:

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

这对我工作:

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

另一个解决scheme:使用flexjson.jar: http ://mvnrepository.com/artifact/net.sf.flexjson/flexjson/3.2

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

简单scheme:

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