0

I have a JSON string and U want to get the name and add fields. I've tried to use several libraries and follow many tutorials, but was unsuccessful.

I thing my problem is that i have several arrays together...

inputLine =
{"posts":[
    {"post":{
        "name":"name1",
        "add":"add1"}},
    {"post":{
        "name":"name2",
        "add":"add2"}}
    ]
}
JSONObject obj_posts = new JSONObject(inputLine);
JSONArray menuitemArray = obj_posts.getJSONArray("posts");
JSONObject obj_post = new JSONObject(menuitemArray.getJSONObject(0).toString());
JSONObject menuitem = obj_post.getJSONObject("post");
JSONArray obj_post1 = menuitem.names();

At this point I can only access the key name and add, not the values.

5
  • 2
    Do you get a particular error? (I do note that your inputLine isn't declared as a String, and if it was, it doesn't properly escape the quotes...) Commented Apr 29, 2014 at 5:55
  • You need to go one more level down in your code to retrieve the values of name and add. But this is really a very crude way of parsing json. I would recommend you to write a java class to represent your json structure and then use a parser such as jackson to un-marshal to your json to java object. Commented Apr 29, 2014 at 5:57
  • 3
    Don't use new JSONObject the second time. You don't need to parse the "inner JSON". Commented Apr 29, 2014 at 5:57
  • menuitemArray.getJSONObject(0).getJSONObject("post"); Commented Apr 29, 2014 at 6:01
  • Thank you Brian Roach, your line solve my problem, and now i will do what Juned Ahsan said, create a class to represent my JSON structure. Thank you all! you are the best! Have a nice day! Commented Apr 29, 2014 at 6:12

3 Answers 3

1
JSONObject obj_posts = new JSONObject(inputLine);
JSONArray menuitemArray = obj_posts.getJSONArray("posts");
JSONObject obj_post = menuitemArray.getJSONObject(0);
JSONObject menuitem = obj_post.getJSONObject("post");
String postName = menuItem.getString("name");
String postAdd = menuItem.getString("add");
Sign up to request clarification or add additional context in comments.

Comments

0

The below code should work:

    System.out.println(menuitem.get(obj_post1.getString(0)));//Output name1
    System.out.println(menuitem.get(obj_post1.getString(1)));//Output add1  

Comments

0

You need to import "org.json.JSONArray" , " org.json.JSONException", "org.json.JSONObject"

Also throws JSONException.

String obj_post1_name = "";
String obj_post1_add = "";
String obj_post2_name = "";
String obj_post2_add = "";
String inputLine =
        " {\"posts\":[{\"post\":{\"name\":\"name1\",\"add\":\"add1\"}},{\"post\":{\"name\":\"name2\",\"add\":\"add2\"}}]}";

        JSONObject obj_posts = new JSONObject(inputLine);
        JSONArray menuitemArray = obj_posts.getJSONArray("posts");
        JSONObject obj_post1 =(menuitemArray.getJSONObject(0));
        JSONObject obj_post2 =(menuitemArray.getJSONObject(1));
        JSONObject menuitem = obj_post1.getJSONObject("post");
        JSONObject menuitem2 = obj_post2.getJSONObject("post");
        obj_post1_name= menuitem.getString("name");
        obj_post1_add= menuitem.getString("add");
        obj_post2_name= menuitem2.getString("name");
        obj_post2_add= menuitem2.getString("add");

or you can use loop after:

JSONArray menuitemArray = obj_posts.getJSONArray("posts");
JSONObject obj_posts;
JSONObject menuitem;
for(int i=0;i<menuitemArray.length();i++){
    obj_posts= menuitemArray.getJSONObject(i);
    menuitem = obj_post1.getJSONObject("post");
    menuitem.getString("name");
    menuitem.getString("add");
}

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.