2013-01-21 43 views
1

冰淇淋三明治,果凍豆將工作得很好。 但是,它不適用於早期版本。Android JSON解析錯誤(Froyo或更少)

我不知道爲什麼。請給我一些建議。

JSON數據:

{ 
"ANDROID" :[ 
    { 
     "NAME" : "homepage", 
     "URL" : "http://www.stackoverflow.com", 
     "IMAGE" : "http://www.stackoverflow/menu_01.png", 
     "USE_YN" : "Y", 
     "APP_YN" : "N" 
    } 
    ]   
} 

錯誤日誌:

org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 

標的來源:

JSONObject jobject = new JSONObject(jsondata);     

謝謝!

+0

你能告訴我們代碼嗎? – Henry

回答

1
/*"ANDROID" :[ 
       { 
        "NAME" : "homepage", 
        "URL" : "http://www.stackoverflow.com", 
        "IMAGE" : "http://www.stackoverflow/menu_01.png", 
        "USE_YN" : "Y", 
        "APP_YN" : "N" 
       } 
       ]   
      }*/ 

      //here is the code to parse 

      try { 
       String jsondata = "your server response like above statements"; 

       if (jsondata != null && !jsondata.equals("") 
         && jsondata.equalsIgnoreCase("null")) { 
        JSONObject jobject = new JSONObject(jsondata); 

        if (jobject != null) { 
         if (jobject.has("ANDROID")) { 
          JSONArray jsonArr = jobject.getJSONArray("ANDROID"); 

          if (jsonArr != null && jsonArr.length() > 0) { 
           for (int i = 0; i < jsonArr.length(); i++) { 
            JSONObject json = jsonArr.getJSONObject(i); 

            if (json != null) { 

             if (json.has("NAME")) { 
              String name = json 
                .getString("NAME"); 
             } 

             if (json.has("URL")) { 
              String url = json.getString("URL"); 
             } 

             if (json.has("IMAGE")) { 
              String image = json 
                .getString("IMAGE"); 
             } 

             if (json.has("USE_YN")) { 
              String use = json 
                .getString("USE_YN"); 
             } 

             if (json.has("APP_YN")) { 
              String app = json 
                .getString("APP_YN"); 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      }