8

I want to parse these kind of Json responses :

{
"MyResponse": {
    "count": 3,
    "listTsm": [{
        "id": "b90c6218-73c8-30bd-b532-5ccf435da766",
        "simpleid": 1,
        "name": "vignesh1"
    },
    {
        "id": "b90c6218-73c8-30bd-b532-5ccf435da766",
        "simpleid": 2,
        "name": "vignesh2"
    },
    {
        "id": "b90c6218-73c8-30bd-b532-5ccf435da766",
        "simpleid": 3,
        "name": "vignesh3"
    }]
 }
}

I tried using SIMPLE JSON parser but this is not working for me:

Object obj = parser.parse(resp);
JSONObject jsonObject = (JSONObject) obj;
JSONArray response = (JSONArray) jsonObject.get("MyResponse");

//JSONArray arr=new JSONArray(yourJSONresponse);
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<response.size(); i++){
    list.add(response.get(i)("name"));
}
5
  • The JSON message isn't valid JSON so that may be your problem. I just checked your JSON string using jsonlint.com Commented Sep 19, 2013 at 15:45
  • Thanks @Sotirios Delimanolis i edited now Commented Sep 19, 2013 at 15:45
  • 1
    @nickebbitt Some parsers accept the ,. Commented Sep 19, 2013 at 15:46
  • 1
    there is no listTsmResponse key in your json ... Commented Sep 19, 2013 at 15:46
  • @nickebbitt i edited and checked in jsonlint.com Commented Sep 19, 2013 at 15:50

2 Answers 2

28
public static void main(String[] args) throws JSONException {
    String jsonString  = "{" + 
            "   \"MyResponse\": {" + 
            "       \"count\": 3," + 
            "       \"listTsm\": [{" + 
            "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
            "           \"simpleid\": 1," + 
            "           \"name\": \"vignesh1\"" + 
            "       }," + 
            "       {" + 
            "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
            "           \"simpleid\": 2," + 
            "           \"name\": \"vignesh2\"" + 
            "       }," + 
            "       {" + 
            "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
            "           \"simpleid\": 3," + 
            "           \"name\": \"vignesh3\"" + 
            "       }]" + 
            "   }" + 
            "}";


    JSONObject jsonObject = new JSONObject(jsonString);
    JSONObject myResponse = jsonObject.getJSONObject("MyResponse");
    JSONArray tsmresponse = (JSONArray) myResponse.get("listTsm");

    ArrayList<String> list = new ArrayList<String>();

    for(int i=0; i<tsmresponse.length(); i++){
        list.add(tsmresponse.getJSONObject(i).getString("name"));
    }

    System.out.println(list);
}   
}

Output:

[vignesh1, vignesh2, vignesh3]

Comment: I didn't add validation

[EDIT]

other way to load json String

    JSONObject obj= new JSONObject();
    JSONObject jsonObject = obj.fromObject(jsonString);
    ....
Sign up to request clarification or add additional context in comments.

5 Comments

am getting error like "The constructor JSONObject(String) is undefined"
... JSONObject jsonObject = new JSONObject(); JSONObject myResponse = jsonObject.getJSONObject("MyResponse"); ...
which version You are using
@MaximShoustin can you send a direct link? Couldn't find related results.
This answer doesn't provide the required import to use the JSONObject class.
-1

You can do simply:

JSONObject response = new JSONObject(resp);

Then you can use depending on the type of the variable something like:

int count = response.getint("count");

or

JSONArray tsm = response.getJSONArray(listTsm)

Then if you want to iterate through the objects inside you use just a for for it.

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.