JSON:
{
"prop":"property",
"inputInfo":{
"sku":"6157068",
"inputText":"iphone"
}
}代码:
JSONObject inputObject = JSON.parseObject(input);
String prop = (String)inputObject.get("property");但是如何获得'sku‘和'inputText’的内层呢?
我正在使用Java的阿里巴巴json库。
发布于 2018-05-11 15:11:33
我还没有用过阿里巴巴的库,但是它没有可以在inputObject上使用的getJSONObject()方法吗?我以前也这样做过,但我使用的是org.json库。
JSON:
{"circle":{
"radius":255819.07998349078,
"center":{
"lat":00.000000000000000,
"lng":00.000000000000000
}
}
}Java
JSONObject shape = new JSONObject(entity.getJsonLocation());
double latitude = shape.getJSONObject("circle")
.getJSONObject("center")
.getDouble("lat");
double longitude = shape.getJSONObject("circle")
.getJSONObject("center")
.getDouble("lng");例如,本例获取JSON并创建一个JSONObject shape。然后,我可以通过在shape上调用getJSONObject()来获取内部json对象。
我希望这能对你有所帮助。
发布于 2018-05-11 14:39:57
您可以首先创建一个bean,例如,
public class DemoInfo {
private String id;
private String city;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}然后,
String s = "{\"id\":\"0375\",\"city\":\"New York\"}";
DemoInfo info = JSON.parseObject(s, DemoInfo.class); 或者,您也可以使用map。
JSON.parseObject(s, HashMap.class);发布于 2018-05-11 15:08:53
你可以这样做,
JSONObject inputInfoObject = inputObject.getJSONObject("inputInfo");
String sku = inputInfoObject.getString("sku");
String inputText = inputInfoObject.getString("inputText");https://stackoverflow.com/questions/50285947
复制相似问题