java中的json从列表中获取正确的值
我正在使用包java中的json从列表中获取正确的值,java,android,json,wcf,Java,Android,Json,Wcf,我正在使用包org.jsonpackage:我需要帮助从java中的json获取corect数据。这是json中的字符串: {"GetLocationsResult":[{"ID":82,"Name":"Malmo","isCity":true,"isCounty":false,"isDisctrict":false,"ID_Parent":null,"ID_Map":35,"ZipCode":"7000"},{"ID":82,"Name":"Trelleborg","isCity":true,
org.json
package:我需要帮助从java中的json获取corect数据。这是json中的字符串:
{"GetLocationsResult":[{"ID":82,"Name":"Malmo","isCity":true,"isCounty":false,"isDisctrict":false,"ID_Parent":null,"ID_Map":35,"ZipCode":"7000"},{"ID":82,"Name":"Trelleborg","isCity":true,"isCounty":false,"isDisctrict":false,"ID_Parent":null,"ID_Map":35,"ZipCode":"7000"}]}
这是一个列表,这只是一个测试,它将包含2个以上的项目,所以我的问题是,我想获得所有位置的名称,我想在我的android应用程序中用名称填充微调器
我怎样才能得到“名字”:“马尔默”等等。。。。
?答案很简单……JSON元素以一个JSON对象
{
开始,而GetLocationsResults
是一个JSON对象的JSON数组。本质上,我将JSON字符串转换为以下代码
JSONObject rootJson = new JSONObject(jsonString);
JSONArray jsonArray = rootJson.getJSONArray("GetLocationsResult");
//Let's assume we need names....
String[] names = null;
if (jsonArray != null) {
names = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
names[i] = json.getString("Name");
}
}
//Test
for (String name: names) {
System.out.println(name);
}
jsonobjectrootjson=newjsonobject(jsonString);
JSONArray JSONArray=rootJson.getJSONArray(“GetLocationsResult”);
//假设我们需要名字。。。。
字符串[]名称=null;
if(jsonArray!=null){
名称=新字符串[jsonArray.length()];
for(int i=0;i