java中字符串数组的JSON字符串
使用上述代码,可获得以下输出:java中字符串数组的JSON字符串,java,json,Java,Json,使用上述代码,可获得以下输出: JSONArray cities = json.getJSONArray("city"); 如何仅从JSON数组到字符串数组获取名称 e、 g.:String[]s={“伦敦”、“马德里”、“巴黎”、“苏黎世”}城市是一个JSONObject数组。迭代JSONObject数组,并从每个数组中获取“name”属性。请参阅@pb2q的答案,其中的代码是为您编写的。//您可能应该在问题中提到您使用的json库 { "id":"1", "name":"London" "
JSONArray cities = json.getJSONArray("city");
如何仅从JSON数组到字符串数组获取名称
e、 g.:
String[]s={“伦敦”、“马德里”、“巴黎”、“苏黎世”}
城市
是一个JSONObject数组。迭代JSONObject数组,并从每个数组中获取“name”
属性。请参阅@pb2q的答案,其中的代码是为您编写的。//您可能应该在问题中提到您使用的json库
{
"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"
}
String[]cities=新字符串[cities.length()];
for(int i=0;i通过JSONArray
循环,并拉出“name”
字段。这类似于您的json.getJSONArray(“city”);
调用,仅在循环中:
// 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");
}
JSONArray cities=json.getJSONArray(“城市”);
JSONObject city=null;
String[]s=新字符串[cities.length()];
对于(int i=0;i
您可以尝试使用类似的库
代码如下所示:
JSONArray cities = json.getJSONArray("city");
JSONObject city = null;
String[] s = new String[cities.length()];
for (int i = 0; i < cities.length(); i++)
{
city = cities.getJsonObject(i);
s[i] = city.get("name");
}
String rawJsonString=。。。;
List cities=JsonPath.read(rawJsonString,“$.city.name”);
使用JsonPath
com.jayway.jsonpath
json路径
0.8.1
你可以得到所有城市的名字
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
String rawJsonString=“…”;
List cities=JsonPath.read(rawJsonString,“$.city[*].name”);
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
String rawJsonString = "...";
List<String> cities = JsonPath.read(rawJsonString, "$.city[*].name");