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

时间:2018-07-18 05:41:29

标签: java arrays 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" +
    "        \"ownership_of_assets\": \"\",\n" +
    "        \"pep\": \"\",\n" +
    "        \"source_of_income\": \"\"\n" +
    "      }},\n" +
    "     {\n" +
    "      \"assets\": {\n" +
    "        \"asset_since\": \"\",\n" +
    "        \"asset_type\": \"\",\n" +
    "        \"comments\": \"\",\n" +
    "        \"estimated_value\": \"\",\n" +
    "        \"material_status\": \"SINGLE\",\n" +
    "        \"ownership_of_assets\": \"\",\n" +
    "        \"pep\": \"\",\n" +
    "        \"source_of_income\": \"\"\n" +
    "      }}]}";
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(response);

        JSONObject jsonObject = (JSONObject) obj;
        //System.out.println(jsonObject.toString());
        System.out.println("json size=="+jsonObject.size());
        System.out.println("hghgfh"+jsonObject.keySet());
         Long sflag = (Long) jsonObject.get("responseFlag");
         String msg=(String) jsonObject.get("responseMsg");
         String resobj=(String) jsonObject.get("responseObj").toString();
        //jsonObject.

        System.out.println("sflag=="+sflag);
        System.out.println("msg=="+msg);
         System.out.println("msg=="+resobj);




  //             JSONArray msg = (JSONArray) jsonObject.get("responseFlag");
  //            Iterator<String> iterator = msg.iterator();
  //            while (iterator.hasNext()) {
  //                System.out.println(iterator.next());
  //            }

  //            for(Iterator iterator = jsonObject.keySet().iterator();     iterator.hasNext();) {
 //    String key = (String) iterator.next();
 //    System.out.println(jsonObject.get(key));
 //}

 //             String asset = (String) jsonObject.get("assets");
 //            System.out.println("session token"+asset);
         //sflag = (Long) jsonObject.get("responseFlag");
        //System.out.println("session sflag"+sflag);

    } catch (ParseException ex) {
       System.out.println(ex);
    }
    }

    }

响应对象是

  [{
    "assets": {
        "comments": "",
        "asset_since": "",
        "material_status": "SINGLE",
        "source_of_income": "",
        "ownership_of_assets": "",
        "asset_type": "",
        "pep": "",
        "estimated_value": ""
    }
}, {
    "assets": {
        "comments": "",
        "asset_since": "",
        "material_status": "SINGLE",
        "source_of_income": "",
        "ownership_of_assets": "",
        "asset_type": "",
        "pep": "",
        "estimated_value": ""
    }
}]

我需要将每个资产值存储在数组中。

2 个答案:

答案 0 :(得分:2)

这是伪代码。您可以在此代码中填写缺少的部分。

       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":""}}]}";

        JSONObject jsonObject = new JSONObject(json); 
        JSONArray jsonArray = jsonObject.getJSONArray("responseObj");


        for(int i=0; i<jsonArray.length(); i++) 
        {
            JSONObject arrayJsonObject  = jsonArray.getJSONObject(i);   
            //insert into your list or array 
        }

答案 1 :(得分:1)

如果您使用的是json-simple-1.1.1 jar。这是下面的代码:

        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(response);
            JSONObject jsonObject = (JSONObject) obj;
            //System.out.println(jsonObject.toString());
            System.out.println("json size==" + jsonObject.size());
            System.out.println("hghgfh" + jsonObject.keySet());
            JSONArray jsonArray = (JSONArray)jsonObject.get("responseObj");
            for(int i=0; i<jsonArray.size(); i++)
            {
                JSONObject arrayJsonObject  = (JSONObject) jsonArray.get(i);
                JSONObject assets = (JSONObject) arrayJsonObject.get("assets");
                // read the assets to store
            }
        }catch (Exception e){

        }