我在java中解析一个简单的json时遇到了问题。这是json的样本。
[
{
"politics": [
{
"type": "admin2",
"friendly_type": "country",
"name": "United States",
"code": "usa"
},
{
"type": "admin6",
"friendly_type": "county",
"name": "Gratiot",
"code": "26_057"
},
{
"type": "constituency",
"friendly_type": "constituency",
"name": "Eighth district, MI",
"code": "26_08"
},
{
"type": "admin6",
"friendly_type": "county",
"name": "Clinton",
"code": "26_037"
},
{
"type": "admin4",
"friendly_type": "state",
"name": "Michigan",
"code": "us26"
},
{
"type": "constituency",
"friendly_type": "constituency",
"name": "Fourth district, MI",
"code": "26_04"
}
],
"location": {
"latitude": 43.111976,
"longitude": -84.71275
}
}
]
现在这给了我正确的json索引。
JSONParser parser = new JSONParser();
Object obj = parser.parse(output);
JSONArray array = (JSONArray)obj;
String jsonobj = array.get(0).toString();
{"politics":[{"code":"usa","name":"United States","type":"admin2","friendly_type":"country"},{"code":"26_057","name":"Gratiot","type":"admin6","friendly_type":"county"},{"code":"26_08","name":"Eighth district, MI","type":"constituency","friendly_type":"constituency"},{"code":"26_037","name":"Clinton","type":"admin6","friendly_type":"county"},{"code":"us26","name":"Michigan","type":"admin4","friendly_type":"state"},{"code":"26_04","name":"Fourth district, MI","type":"constituency","friendly_type":"constituency"}],"location":{"latitude":43.111976,"longitude":-84.71275}}
但我似乎无法从中得到我想要的属性。
JSONObject obj1 = new JSONObject(jsonobj);
String n = obj1.getString("admin4");
System.out.println(n);
我所需要的就是这个json就是密歇根州。我哪里错了? 非常感谢帮助。
答案 0 :(得分:0)
简化json对象上的搜索和其他操作的最佳解决方案是将json字符串转换为java对象并进行操作。
将json字符串转换为java对象使用跟随代码:
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
YourObject myObject;
ObjectMapper mapper = new ObjectMapper();
try{
myObject= mapper.readValue(jsonData, myObject.class);
}
catch (Exception e) {
e.printStackTrace();
}
例如定义你的班级屁股跟随:
public class myObject{
private List<Politics> politics;
private Location location;
// define getters and setters
}
define Politics and Location class:
public class Politics
{
String type;
String friendly_type;
String name;
String code;
// define getters and setters
}
public class Location
{
String latitude;
String longitude;
// define getters and setters
}
答案 1 :(得分:0)
首先,array.get(0)
将为您提供主数组中的第一个元素。第一个元素是一个JSON对象,它有两个属性politics
和location
。您似乎对politics
属性的数组值内的值感兴趣。您必须使用此((JSONArray)((JSONObject)array.get(0)).get("politics"))
来获取该数组。
其次,admin4
不是属性,它实际上是type
属性的值。你必须遍历数组才能找到它。
这是一个完整的例子:
JSONParser parser = new JSONParser();
Object obj = parser.parse(output);
JSONArray array = (JSONArray)obj;
JSONArray politics = ((JSONObject)array.get(0)).get("politics"));
JSONObject obj = null;
for(int i = 0; i < politics.size(); i++){
if(((JSONObject)politics.get(i)).getString("type").equals("admin4")){
obj = ((JSONObject)politics.get(i));
}
}
if(obj != null){
// Do something with the object.
}
您似乎正在使用simple json
库。我不记得确切的是.get("politics")
还是.getJSONObject("politics")
。在我的例子中,方法名称可能存在其他错误。
答案 2 :(得分:0)
这是因为您正在尝试获取JSON对象的内部元素。 尝试
JSONObject obj1 = new JSONObject(jsonobj);
JSONArray arr = (JSONArray) obj1.getObject("politics");
您将获得一个JSONArray对象,该对象进一步构成JSON对象。 现在,为了使用键获取值,您必须迭代数组,如下所示:
for(int i=0; i<arr.size(); i++){
JSONObject obj = arr.getJSONArray(i);
System.out.println(obj.getString("type"));
}
现在将为您提供输出:
admin2
admin6
constituency
admin6
admin4
constituency