2012-09-18 85 views
-1

我正在應用程序中獲得JSON響應。的反應是如下:如何解析JSON響應

[ 
    { 
     "CID": 5, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "", 
     "Name": "Jewelry", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    }, 
    { 
     "CID": 122, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "", 
     "Name": "Collection", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    }, 
    { 
     "CID": 459, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "http://jewelry.roughdiamondxchange.in/Guide.aspx", 
     "Name": "Education", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    }, 
    { 
     "CID": 483, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "", 
     "Name": "Design Your Own ", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    }, 
    { 
     "CID": 486, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "http://jewelry.roughdiamondxchange.in/About.aspx", 
     "Name": "Policy", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    }, 
    { 
     "CID": 56, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "", 
     "Name": "Diamonds", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "YES" 
    }, 
    { 
     "CID": 611, 
     "ChildMenus": [], 
     "Level": null, 
     "MenuUrl": "", 
     "Name": "Overview", 
     "PCID": 235, 
     "REnum": null, 
     "isLooseDiamond": "NO" 
    } 
] 

爲了解析此我用下面的代碼

try { 
    // Getting Array of Contacts 
    /*JSONObject jsonObject = new JSONObject(json);*/ 
    /*JSONArray array = json.getJSONArray("Value");*/ 
    JSONArray array=new JSONArray(); 
    array.put(getJSONFromUrl()); 

    // looping through All Contacts 
    for(int i = 0; i < array.length(); i++){ 
     JSONObject c = array.getJSONObject(i); 
     /* JSONObject object = jsonObject.getJSONObject("FirstObject");*/ 
     // Storing each json item in variable 
     /*String cid = c.getString(TAG_CID);*/ 
     String name = c.getString(TAG_NAME); 
     String pcid = c.getString(TAG_PCID); 
     String menuurl = c.getString(TAG_MENUURL); 

     // Phone number is agin JSON Object 

     // creating new HashMap 
     HashMap<String, String> map = new HashMap<String, String>(); 

     // adding each child node to HashMap key => value 
     /* map.put(TAG_CID, cid);*/ 
     map.put(TAG_NAME, name); 
     map.put(TAG_PCID, pcid); 
     map.put(TAG_MENUURL, menuurl); 

     // adding HashList to ArrayList 
     menuList.add(map); 
    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 

它給我一個org.json.JSONException: No value for name錯誤。我無法解析數據。我將不勝感激關於如何解析日期的任何想法。

+0

調試。看'c'變量 – Ilya

+0

@llya:c:{「Name」:「Jewelry」,「ChildMenus」的值:[],「REnum」:null,「isLooseDiamond」:「NO」,「MenuUrl」 」, 「等級」:空, 「PCID」:235, 「CID」:5} – sad

回答

0

檢查字符串TAG_NAME =「姓名」而不是「名」

0

當您嘗試訪問TAG_NAME這是不是在JSONObject的會出現此錯誤。 檢查存在TAG_NAME的由具有(TAG_NAME)返回布爾

字符串名稱= c.getString(TAG_NAME);

0

我想你需要檢查字符串TAG_NAME。將代碼更改爲以下代碼後更改。希望它能幫助你。

for(int i = 0; i < array.length(); i++){ 
      JSONObject c = array.getJSONObject(i); 
      /* JSONObject object = jsonObject.getJSONObject("FirstObject");*/ 
      // Storing each json item in variable 
      /*String cid = c.getString(TAG_CID);*/ 
      String name = c.getString("Name"); 
      String pcid = c.getString("PCID"); 
      String menuurl = c.getString("MenuUrl"); 

      // Phone number is agin JSON Object 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      /* map.put(TAG_CID, cid);*/ 
      map.put("Name", name); 
      map.put("PCID", pcid); 
      map.put("MenuUrl", menuurl); 

      // adding HashList to ArrayList 
      menuList.add(map); 
     } 
0

試試這個

     List<String>mCid = new ArrayList<String>(); 
     List<String>mLevel = new ArrayList<String>(); 
     String value,result; 
     InputStream is,in; 
     JSONArray jArray = null; 

      try { 

        HttpClient httpclient=new DefaultHttpClient(); 
       HttpPost httppost=new HttpPost("url"); 
       //namevaluepair=new ArrayList<NameValuePair>(1); 
       //namevaluepair.add(new BasicNameValuePair("key",value));//if you want to send data to server 

       //httppost.setEntity(new UrlEncodedFormEntity(namevaluepair)); 
       HttpResponse httpresponse=httpclient.execute(httppost); 
       in=httpresponse.getEntity().getContent(); 
      //inputStream = response.getEntity().getContent(); 
      }catch(Exception e){Log.e("","error in http connection");} 

      try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8); 
       StringBuilder sb1 = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb1.append(line + "\n"); 
       } 
       in.close(); 
       result=sb1.toString(); 

         Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); 
         try{ 
         JSONObject json=new JSONObject(result); 

         jArray=json.getJSONArray("Array_name_in_response"); //whatever name of your array in response 
         System.out.println(jArray.length()); 

         //if(jArray.length()!=0){ 
         for(int i=0;i<jArray.length();i++) 
         { 

         value = jArray.getJSONObject(i).optString("CID"); 
         mCid.add(value); 
         value = jArray.getJSONObject(i).optString("Level"); 
         mLevel.add(value); 
         System.out.println(value); 
         } 
        // } 

        }catch(JSONException e){ 
         Log.e("log_tag", " Error parsing data "+e.toString()); 
         } 


     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     }