从java中的json数组中拆分json对象 [英] split json object from json array in java

查看:108
本文介绍了从java中的json数组中拆分json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将API调用发送到返回这样的json数组的服务:

I am sending API call to a service that return a json array like this :

[Object, Object ....]

通过我的Java http 请求

.结果保存在字符串中:

via my java http request. the resulat are stored in a string:

StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

我需要找到一种方法来通过json对象对该字符串进行 split 拆分,以便每个新字符串仅包含一个对象.谢谢.

I need to find a way to split this string to by json objects so each new string will contain only one object. Thanks.

推荐答案

您可以将String转换为 JSONArray ,然后迭代抛出,而不必使用 split 函数数组

Instead of using the split function, you can convert your String to a JSONArray and then iterate throw the array

JSONArray jsonArray = new JSONArray(response.toString());
for(int i=0; i<jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String jsonObjectAsString = jsonObject.toString();
}

这篇关于从java中的json数组中拆分json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆