如何使用java读取多个json对象?

我有一个 JSON 响应,我想将每个元素存储在一个字符串中。因为我是 JSON 的新手,所以很难找到解决方案。请建议我一个解决方案。以下是我的 json 响应。


{

    "responseFlag": 1,

    "responseMsg": "Successfully retrieved data",

    "responseObj": [{

            "assets": {

                "asset_since": "",

                "asset_type": "",

                "comments": "",

                "estimated_value": "",

                "material_status": "SINGLE",

                "ownership_of_assets": "",

                "pep": "",

                "source_of_income": ""

            }

        },

        {

            "assets": {

                "asset_since": "",

                "asset_type": "",

                "comments": "",

                "estimated_value": "",

                "material_status": "SINGLE",

                "ownership_of_assets": "",

                "pep": "",

                "source_of_income": ""

            }

        }

    ]

}

我想将每个对象元素存储在一个数组中。


我试过的代码如下。


 package mytry;


 import java.util.Iterator;

 import org.json.simple.JSONArray;

 import org.json.simple.JSONObject;

 import org.json.simple.parser.JSONParser;

 import org.json.simple.parser.ParseException;



  public class Mytry {



   public static void main(String[] args) {

    // TODO code application logic here


    String response="{\n" +

    "  \"responseFlag\": 1,\n" +

    "  \"responseMsg\": \"Successfully retrieved data\",\n" +

    "  \"responseObj\": [\n" +

    "    {\n" +

    "      \"assets\": {\n" +

    "        \"asset_since\": \"\",\n" +

    "        \"asset_type\": \"\",\n" +

    "        \"comments\": \"\",\n" +

    "        \"estimated_value\": \"\",\n" +

    "        \"material_status\": \"SINGLE\",\n" +

 

慕莱坞森
浏览 278回答 2
2回答

函数式编程

如果您使用的是 json-simple-1.1.1 jar。这是下面的代码:&nbsp; &nbsp; &nbsp; &nbsp; JSONParser parser = new JSONParser();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object obj = parser.parse(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = (JSONObject) obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(jsonObject.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("json size==" + jsonObject.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("hghgfh" + jsonObject.keySet());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = (JSONArray)jsonObject.get("responseObj");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<jsonArray.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject arrayJsonObject&nbsp; = (JSONObject) jsonArray.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject assets = (JSONObject) arrayJsonObject.get("assets");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // read the assets to store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception e){&nbsp; &nbsp; &nbsp; &nbsp; }

慕仙森

这是一个伪代码。您可以填写此代码中缺少的部分。&nbsp; &nbsp; &nbsp; &nbsp;String json = "{"responseFlag":1,"responseMsg":"Successfully retrieved data","responseObj":[{"assets":{"asset_since":"","asset_type":"","comments":"","estimated_value":"","material_status":"SINGLE","ownership_of_assets":"","pep":"","source_of_income":""}},{"assets":{"asset_since":"","asset_type":"","comments":"","estimated_value":"","material_status":"SINGLE","ownership_of_assets":"","pep":"","source_of_income":""}}]}";&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(json);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = jsonObject.getJSONArray("responseObj");&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<jsonArray.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject arrayJsonObject&nbsp; = jsonArray.getJSONObject(i);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //insert into your list or array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java