将InputStream转换为JSONObject

时间:2014-03-17 17:51:18

标签: java android json inputstream

我使用以下代码将InputStream转换为JSONObject。我的问题是,是否有任何简单的方法将InputStream转换为JSONObject。不做InputStream - > BufferedReader - > StringBuilder - >循环 - > 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());

13 个答案:

答案 0 :(得分:20)

由于您已经在使用Google的Json-Simple库,因此您可以像InputStream那样解析json:

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

答案 1 :(得分:10)

使用JsonReader来解析InputStream。请参阅API内的示例: http://developer.android.com/reference/android/util/JsonReader.html

答案 2 :(得分:7)

如果你不想弄乱现成的图书馆,你可以创建这样的课程。

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

答案 3 :(得分:5)

此代码有效

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

答案 4 :(得分:2)

您可以使用实体:

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

答案 5 :(得分:2)

简单解决方案:

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

答案 6 :(得分:2)

使用Jackson JSON解析器

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(inputStreamObject,Map.class);

如果要专门使用JSONObject,则可以转换地图

JSONObject json = new JSONObject(map);

有关JSONObject构造函数http://stleary.github.io/JSON-java/index.html

的用法,请参考此内容

答案 7 :(得分:1)

您可以使用此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"
}

答案 8 :(得分:1)

这对我有用:

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

答案 9 :(得分:1)

这是一种不使用循环且仅使用Android API的解决方案:

InputStream inputStreamObject = PositionKeeperRequestTest.class.getResourceAsStream(jsonFileName);
byte[] data = new byte[inputStreamObject.available()];
if(inputStreamObject.read(data) == data.length) {
    JSONObject jsonObject = new JSONObject(new String(data));
}

答案 10 :(得分:0)

另一种解决方案:使用flexjson.jar:http://mvnrepository.com/artifact/net.sf.flexjson/flexjson/3.2

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

答案 11 :(得分:0)

我认为最好的解决方案是将InputStream封装在JSONTokener对象中。 像这样:

JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));

答案 12 :(得分:0)

InputStream inputStreamObject = PositionKeeperRequestTest.class.getResourceAsStream(jsonFileName);
JSONObject jsonObject = new JSONObject(IOUtils.toString(inputStreamObject));