1

After completing the XML SAX Parsing now I am working on JSON Parsing in my application. Here I am providing you the Json

Response : {"menu": {
              "id": "file",
              "value": "File",
              "popup": {
                "menuitem": [
                  {"value": "New", "onclick": "CreateNewDoc()"},
                  {"value": "Open", "onclick": "OpenDoc()"},
                  {"value": "Close", "onclick": "CloseDoc()"}
                ]
              }
            }}

Now I had referred one example of Json Parsing in which they had created a jString Object and I had a doubt in that particular line which is as under :

private String jString = "{\"menu\":    {\"id\": \"file\", \"value\": \"File\", \"

Can anyone make me clear about it please. The Link of the Complete Example is as Under:

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

2 Answers 2

2

You can get a JSON object from the response like this (I assume you know how to take care of the response):

String[] file = null;

/* Make a JSON object from your response, yourResponse is a String containing
the whole response */
JSONObject jsonObject = new JSONObject(yourResponse);

/* Your "menu": array from your response */
JSONArray infoArray = jsonObject.getJSONArray("menu"); 

/* If the array contains multiple items, loop it
through the infoArray's size */
for (int i = 0; i < infoArray.length(); i++) {  
        try {    
          //Get the value inside "id"
          file[i] = infoArray.getJSONObject(i).optString("id");                  
            } catch (JSONException e) {
            }       
}
Sign up to request clarification or add additional context in comments.

Comments

1
private String jString = "{\"menu\":    {\"id\": \"file\", \"value\": \"File\", \"

They are just creating a string that will look like:

{"menu":    {"id": "file", "value": "File", .. etc.

They use \ because the " char has to be escaped.

1 Comment

My doubt is that how long we have to type the string? I mean do we have to type the full response in the Object we create? Because my response is quite very long compared to the one in Example.

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.