gpt4 book ai didi

java - java中将json转换为字符串

转载 作者:行者123 更新时间:2023-12-01 23:14:08 24 4
gpt4 key购买 nike

如何将复杂的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com