Java json尝试从json获取特定值很简单
我正在尝试用java对地址进行地理编码,并获取lat/lng坐标。我能够对地址进行地理编码并将其返回到json对象中,但我无法理解如何使用json simple解析下面的json以获得lat/lng。感谢您的帮助Java json尝试从json获取特定值很简单,java,json,json-simple,Java,Json,Json Simple,我正在尝试用java对地址进行地理编码,并获取lat/lng坐标。我能够对地址进行地理编码并将其返回到json对象中,但我无法理解如何使用json simple解析下面的json以获得lat/lng。感谢您的帮助 { "results" : [ { "address_components" : [ { "long_name" : "Minneapolis", "short_na
{
"results" : [
{
"address_components" : [
{
"long_name" : "Minneapolis",
"short_name" : "Minneapolis",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Hennepin County",
"short_name" : "Hennepin County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Minnesota",
"short_name" : "MN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Minneapolis, MN, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.05125,
"lng" : -93.193794
},
"southwest" : {
"lat" : 44.890144,
"lng" : -93.32916299999999
}
},
"location" : {
"lat" : 44.983334,
"lng" : -93.26666999999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.05125,
"lng" : -93.193794
},
"southwest" : {
"lat" : 44.890144,
"lng" : -93.32916299999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
我尝试了很多不同的方法,但这是我最近的失败:
JSONObject json = (JSONObject)parser.parse(addressAsString);
JSONObject results = (JSONObject) json.get("results"); <-- LINE 186
JSONArray geometry = (JSONArray) results.get("geometry");
for(int i = 0; i < geometry.size(); i++) {
JSONObject p = (JSONObject) geometry.get(i);
System.out.println(p);
}
JSONObject json=(JSONObject)parser.parse(addressAsString);
JSONObject结果=(JSONObject)json.get(“结果”) 这是因为“结果”是一个JSONArray。尝试:
JSONArray results = (JSONArray ) json.getJSONArray("results");
“results”是一个JSONArray,其中填充了(在本例中只有一个)JSONObject。这些JSONObject包含您期望的JSONArray“几何体”。以下是修改后的代码,因此它会循环遍历结果并打印几何体数据:
JSONObject json = (JSONObject)parser.parse(addressAsString);
JSONArray results = (JSONArray) json.get("results");
for (int i = 0; i < results.size(); i++) {
// obtaining the i-th result
JSONObject result = (JSONObject) results.get(i);
JSONObject geometry = (JSONObject) result.get("geometry");
JSONObject location = (JSONObject) geometry.get("location");
System.out.println(location.get("lat"));
System.out.println(location.get("lng"));
}
JSONObject json=(JSONObject)parser.parse(addressAsString);
JSONArray结果=(JSONArray)json.get(“结果”);
对于(int i=0;i
让我来为您详细介绍一下,以便您能够理解JSON中的{}表示对象[]表示数组。简单?嗯
视觉细分
results
index 0 ->
addressed_components(array)-->
long_name(single entity)
short_name(single entity)
types(array)
formatted_address(single entity)
geometry(huge ass object with nested objects)
types(array)
- 基本上,在您现在拥有的代码中,结果0索引将包含“地址组件”、“格式化地址”、“几何体”、“类型”。(注意,这都是一个对象)
- “地址\组件”是一个数组
- 其中包含多个“地址组件”
- “几何体”只是一个非常大的对象,其中有许多不同的属性
- 最后是一个数组
psuedo从数组中检索项目的代码,哟
LOOP THROUGH JSON ARRAY
ASSIGN OBJECT VALUES // getting different objects that are indexed in the array.
(如果您想查看代码以了解这一切是如何完成的,请告诉我)
德国劳埃德船级社 我使用net.sf.json
库,但您可以使用逻辑
以下是代码:
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
public class Test {
static String str = "{ \"results\" : [ { \"address_components\" : [ { \"long_name\" : \"Minneapolis\", \"short_name\" : \"Minneapolis\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"Hennepin County\", \"short_name\" : \"Hennepin County\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"Minnesota\", \"short_name\" : \"MN\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"United States\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Minneapolis, MN, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 45.05125, \"lng\" : -93.193794 }, \"southwest\" : { \"lat\" : 44.890144, \"lng\" : -93.32916299999999 } }, \"location\" : { \"lat\" : 44.983334, \"lng\" : -93.26666999999999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 45.05125, \"lng\" : -93.193794 }, \"southwest\" : { \"lat\" : 44.890144, \"lng\" : -93.32916299999999 } } }, \"types\" : [ \"locality\", \"political\" ] } ], \"status\" : \"OK\"}";
public static void main(String[] args) {
parseAndCheckJsonObj(str, "");
}
static void parseAndCheckJsonObj(Object str, Object key) {
/*
* Check whether str is Valid JSON
* String i.e. started by { [ or not
*/
if (JSONUtils.mayBeJSON(str.toString())) {
try {
if (JSONUtils.isArray(str)) {
/*if block Check for str as a Json Array*/
JSONArray rootArr = JSONArray.fromObject(str);
for (int i = 0; i < rootArr.size(); i++) {
parseAndCheckJsonObj(rootArr.get(i), key);
}
} else {
/*else block Check for str as a Json Object*/
JSONObject rootObj = JSONObject.fromObject(str);
Iterator keyIter = rootObj.keys();
while (keyIter.hasNext()) {
Object objKey = keyIter.next();
parseAndCheckJsonObj(rootObj.get(objKey), objKey);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
if (key.equals("lat"))
System.out.println("Latitude is : " + str);
else if (key.equals("lng"))
System.out.println("Longitude is : " + str);
else
System.out.println(key + " : " + str);
}
}
}
如果我更改了这一点,那么下一行就会出现错误:“类型ArrayList中的方法get(int)不适用于参数(字符串)”
是,这是因为您将结果更改为JSONArray而不是JSONObject。您需要遍历JSONArray才能获得JSONObject。您如何知道什么应该是JSONObect,什么应该是JSONArray?您可以通过查看每个冒号后的标点来判断。如果标点符号是{,则冒号左边的值是JSONObject。如果标点符号是[则冒号左边的值是JSONArray。在您的示例中:“结果”和“地址组件”都是JSONArray、“几何体”和“边界”是JSONObject。我经常编辑我的答案,我会看一看。看一看我的答案可能会对你有所帮助。如果你有特殊要求,请告诉我。这会导致异常org.json.simple.JSONObject无法转换为org.json.simple.JSONArray
第JSONArray geometry=(JSONArray)result.get行(“geometry”);
你说得对,对不起。我编辑了我的答案。很高兴你已经找到了解决方案。这是一个很好的答案,但我不需要递归的方法来获取所有的值。我只是专门寻找lat/lng,仅此而已。对对象和数组的解释很有帮助。一旦我理解了这一点,我就能够找到它。
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
public class Test {
static String str = "{ \"results\" : [ { \"address_components\" : [ { \"long_name\" : \"Minneapolis\", \"short_name\" : \"Minneapolis\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"Hennepin County\", \"short_name\" : \"Hennepin County\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"Minnesota\", \"short_name\" : \"MN\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"United States\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Minneapolis, MN, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 45.05125, \"lng\" : -93.193794 }, \"southwest\" : { \"lat\" : 44.890144, \"lng\" : -93.32916299999999 } }, \"location\" : { \"lat\" : 44.983334, \"lng\" : -93.26666999999999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 45.05125, \"lng\" : -93.193794 }, \"southwest\" : { \"lat\" : 44.890144, \"lng\" : -93.32916299999999 } } }, \"types\" : [ \"locality\", \"political\" ] } ], \"status\" : \"OK\"}";
public static void main(String[] args) {
parseAndCheckJsonObj(str, "");
}
static void parseAndCheckJsonObj(Object str, Object key) {
/*
* Check whether str is Valid JSON
* String i.e. started by { [ or not
*/
if (JSONUtils.mayBeJSON(str.toString())) {
try {
if (JSONUtils.isArray(str)) {
/*if block Check for str as a Json Array*/
JSONArray rootArr = JSONArray.fromObject(str);
for (int i = 0; i < rootArr.size(); i++) {
parseAndCheckJsonObj(rootArr.get(i), key);
}
} else {
/*else block Check for str as a Json Object*/
JSONObject rootObj = JSONObject.fromObject(str);
Iterator keyIter = rootObj.keys();
while (keyIter.hasNext()) {
Object objKey = keyIter.next();
parseAndCheckJsonObj(rootObj.get(objKey), objKey);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
if (key.equals("lat"))
System.out.println("Latitude is : " + str);
else if (key.equals("lng"))
System.out.println("Longitude is : " + str);
else
System.out.println(key + " : " + str);
}
}
}
long_name : Minneapolis
short_name : Minneapolis
types : locality
types : political
long_name : Hennepin County
short_name : Hennepin County
types : administrative_area_level_2
types : political
long_name : Minnesota
short_name : MN
types : administrative_area_level_1
types : political
long_name : United States
short_name : US
types : country
types : political
formatted_address : Minneapolis, MN, USA
Latitude is : 45.05125
Longitude is : -93.193794
Latitude is : 44.890144
Longitude is : -93.329163
Latitude is : 44.983334
Longitude is : -93.26666999999999
location_type : APPROXIMATE
Latitude is : 45.05125
Longitude is : -93.193794
Latitude is : 44.890144
Longitude is : -93.329163
types : locality
types : political
status : OK