2012-10-03 23 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免费问任何查询