0

I have a JSON response and I want to store each element in a string. as I am new to JSON, its difficult to find the solution. please suggest me a solution. the below is my json response.

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

I want to store each object elements in an array.

the code I have tried is below.

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

    }

the response object is

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

I need each asset values to be stored in an array.

2
  • What's the actual problem? It's great to have a need but that doesn't constitute a valid question by itself. Commented Jul 18, 2018 at 5:45
  • "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": "" } i want this response objects to be stored in an array Commented Jul 18, 2018 at 5:46

2 Answers 2

2

Here is a pseudo code. You can fill the missing parts in this code.

       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 
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Praveen, im using json-simple-1.1.1 jar. do i need to change the jar?
If json-simple jar, please follow Jaycee answer below. I used json.jar
Welcome. However the overall concept is same. Get jsonArray from the jsonObject and iterate it.
1

If you are using using json-simple-1.1.1 jar. here is the code below:

        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){

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.