gpt4 book ai didi

java - java中JSON字符串到String数组

转载 作者:行者123 更新时间:2023-12-02 07:36:20 24 4
gpt4 key购买 nike

JSONArray cities = json.getJSONArray("city");

使用上面的代码得到以下输出:

{
"id":"1",
"name":"London"
"country":"United Kingdom"
},

{
"id":"2",
"name":"Madrid"
"country":"Spain"
},

{"id":"3",
"name":"Paris"
"country":"France"
},

{
"id":"3",
"name":"Zurich"
"country":"Switzerland"
}

如何仅将 JSON 数组中的名称获取到字符串数组?

例如:String[] s ={"伦敦","马德里","巴黎","苏黎世"}

最佳答案

// you should probably mention what json library you use in your question
String[] cities = new String[cities.length()];
for (int i = 0; i<cities.length(); i++) {
cities[i] = cities.getJsonObject(i).getString("name");
}

关于java - java中JSON字符串到String数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184687/

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