Json Parsing in Android Application

后端 未结 2 1384
你的背包
你的背包 2020-12-07 00:22

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\": {
                  


        
相关标签:
2条回答
  • 2020-12-07 00:25

    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) {
                }       
    }
    
    0 讨论(0)
  • 2020-12-07 00:33
    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.

    0 讨论(0)
提交回复
热议问题