如何将复杂的 json 对象放入字符串数组中?
JSON 示例
{
"FriendsList": [
{
"username": "ChanServ",
"avatar": "http:\/\/clavatars.photos-box.org\/chack.jpg"
},
{
"username": "kroot",
"avatar": "http:\/\/clavatars.photos-box.org\/land2.jpg"
},
{
"username": "morrildl",
"avatar": "http:\/\/clavatars.photos-box.org\/land3.jpg"
},
{
"username": "addo",
"avatar": "http:\/\/clavatars.photos-box.org\/land8.jpg"
}
]
}
这是代码:
protected void onPostExecute(String result) {
super.onPostExecute(result);
JSONObject json;
try {
json = new JSONObject(result);
tvhttp.setText(json.toString(2));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
最佳答案
试试这个
JSONObject json = null;
try {
json = new JSONObject(result);
JSONArray jsonArray = json.getJSONArray("FriendsList");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject jsonObjectData1 = jsonArray.getJSONObject(j);
String username = jsonObjectData1.getString("username");
String avatar = jsonObjectData1.getString("avatar");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
关于java - java中将json转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479713/