2012-10-03 34 views
0

在我的應用程序中,我使用URL調用時得到了Json字符串響應。 JSON字符串迴應是這樣的:Android如何讓Json從Json字符串解析?

jsonFlickrApi({ 
    "user": { 
     "id": "[email protected]", 
     "nsid": "[email protected]", 
     "username": { 
      "_content": "polamreddy_subbareddy" 
     } 
    }, 
    "stat": "ok" 
}) 

從這一點,我想打通解析id值。我會怎麼做?

+4

它不是一個有效的json字符串。只需檢查http://jsonlint.com/ –

+0

編輯:[谷歌是你的朋友](https://www.google.com/#hl=en&sclient=psy-ab&q=json+parsing+android&oq=json+parsing+ android) –

+2

在堆棧溢出問題之前,您應該首先認真對待Google。在http://www.google.com上搜索「json parsing android」。你將得到無限的結果和好的教程。然後,如果你面臨一個問題,那麼來到SO。 – Antrromet

回答

1

試試這個我從這裏得到的解決方案:

thedata = new MyData().GetRequest(url, null, timeout); 
Log.i(TAG, "" +thedata); 

jObject = new JSONObject(thedata); 

JSONObject menuObject = jObject.getJSONObject("user"); 
attributeId = menuObject.getString("id"); 
Log.i(TAG, "" +attributeId); 

並初始化變量:

private final int[] timeout={3,10}; 
private String thedata; 
1

如果你的JSON字符串是..

 { 
     "user":{ 
     "id":"[email protected]", 
     "nsid":"[email protected]", 
     "username":{ 
      "_content":"polamreddy_subbareddy" 
        } 
     }, 
     "stat":"ok" 
    } 

然後解析數據是這樣..

 JSONObject returnResult = jList.getJSONFromUrl(); 

    JSONObject status = returnResult .getJSONObject(stat); 
    JSONObject user = returnResult .getJSONObject(user); 

    String id = user.getString(id); 
    String nsid = user.getString(nsid); 
JSONObject username = user.getJSONObject(username); 
    String contenr = username .getString(content); 

試試這個..feel免費問任何查詢