英文:
Parsing deeply nested object from HTTP API call with Java
问题
我正在调用新加坡政府的公共“taxi-availability”API,试图获取“coordinates”数组的值,该数组是一个对象列表。然而,我在解析响应时遇到了困难,因为它是一个深度嵌套的对象,我不想为其定义一个单独的数据类。
响应数据模型(未显示所有键):
{
features: [
{
geometry: {
coordinates: [
{
}
]
}
}
]
}
获取响应的代码:
String uri = "https://api.data.gov.sg/v1/transport/taxi-availability";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
将响应从字符串转换为对象的代码:
JSONObject obj = new JSONObject(result);
尝试查询“coordinates”的错误尝试:
obj.getJSONArray("features").get(0).getJSONObject("geometry").getJSONArray("coordinates").toString();
警告:“The method getJSONObject(String) is undefined for type object”在“.get(0).getJSONObject("geometry")”处。
请告诉我如何查询对象的“coordinates”数组。我愿意使用不同的库。
英文:
I am calling the Singapore government's public taxi-availability
API and am trying to get the value of the coordinates
array which is a list of objects. However, I am having difficulty parsing the response as it is a deeply nested object and I do not want to define a separate data class for it
Response data model (not all keys are shown):
{
features: [
{
geometry: {
coordinates: [
{
}
]
}
}
]
}
Code to get the response:
String uri = "https://api.data.gov.sg/v1/transport/taxi-availability";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
Code to convert the response from string to an object:
JSONObject obj = new JSONObject(result);
Faulty attempt to query for coordinates
:
obj.getJSONArray("features").get(0).getJSONObject("geometry").getJSONArray("coordinates").toString();
Warning: The method getJSONObject(String) is undefined for type object
at .get(0).getJSONObject("geometry")
Please advise on how I can query for the coordinates
array of objects. I'm willing to use different libraries
答案1
得分: 1
以下是翻译好的内容:
使用此结果示例
{
"type": "FeatureCollection",
"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/", "type": "ogcwkt"}},
"features": [{"type": "Feature", "geometry": {"type": "MultiPoint", "coordinates": [[103.62835, 1.29337], [103.63217, 1.31944] ......]
}
您可以通过以下方式循环遍历结果并使用JSONArrays和JSONObjects提取坐标
JSONObject resultObject = new JSONObject(result);
JSONArray featuresArray = resultObject.getJSONArray("features");
System.out.println("\nfeaturesArray = " + featuresArray);
//[{"geometry":{"coordinates":[[103.62382,1.28178],[103.6265,1.29515],
for (int f = 0; f < featuresArray.length(); f++) {
JSONObject featuresObj = featuresArray.getJSONObject(f);
System.out.println("\nfeaturesObj = " + featuresObj);
//{"geometry":{"coordinates":[[103.62803,1.28839],[103.62911,1.29983],
JSONObject geometryObj = featuresObj.getJSONObject("geometry");
System.out.println("\ngeometryObj = " + geometryObj);
//{"coordinates":[[103.62711,1.30928],[103.62718,1.29478]
JSONArray coordsArray = geometryObj.getJSONArray("coordinates");
System.out.println("\ncoordsArray = " + coordsArray);
//[[103.61466,1.25029],[103.61822,1.27588],
for (int c = 0; c < coordsArray.length(); c++) {
System.out.println("Coord = " + coordsArray.get(c));
//Coord = [103.62387,1.28607]
//Coord = [103.62649,1.29515]
}
}
希望这对您有所帮助。
英文:
With this result sample
{
"type":"FeatureCollection",
"crs":{"type":"link","properties":{"href":"http://spatialreference.org/ref/epsg/4326/ogcwkt/","type":"ogcwkt"},
"features":[{"type":"Feature","geometry":{"type":"MultiPoint","coordinates":[[103.62835,1.29337],[103.63217,1.31944]
......
you can loop through result and extract coordinates using JSONArrays & JSONObjects as below
JSONObject resultObject = new JSONObject(result);
JSONArray featuresArray = resultObject.getJSONArray("features");
System.out.println("\nfeaturesArray = " + featuresArray);
//[{"geometry":{"coordinates":[[103.62382,1.28178],[103.6265,1.29515],
for (int f = 0; f < featuresArray.length(); f++) {
JSONObject featuresObj = featuresArray.getJSONObject(f);
System.out.println("\nfeaturesObj = " + featuresObj);
//{"geometry":{"coordinates":[[103.62803,1.28839],[103.62911,1.29983],
JSONObject geometryObj = featuresObj.getJSONObject("geometry");
System.out.println("\ngeometryObj = " + geometryObj);
//{"coordinates":[[103.62711,1.30928],[103.62718,1.29478]
JSONArray coordsArray = geometryObj.getJSONArray("coordinates");
System.out.println("\ncoordsArray = " + coordsArray);
//[[103.61466,1.25029],[103.61822,1.27588],
for (int c = 0; c < coordsArray.length(); c++) {
System.out.println("Coord = " + coordsArray.get(c));
//Coord = [103.62387,1.28607]
//Coord = [103.62649,1.29515]
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论