gpt4 book ai didi

java - 从 JSON http 请求中读取特定项目

转载 作者:行者123 更新时间:2023-12-01 11:30:38 25 4
gpt4 key购买 nike

我通过 http 请求从休息服务器 (jax rs) 获取 JSON。

[{"carId":"t0","movements":[{"edge":{"edgeId":"--104","fromNode":{"latitude":"51.404456094285734","longitude":"5.477570348298111","nodeId":"-16"},"lanes":[{"laneId":"--104_0"},{"laneId":"--104_1"}],"toNode":{"latitude":"51.404057790546894","longitude":"5.424575687577547","nodeId":"-14"}},"id":8,"lane":{"laneId":"--104_0"},"position":73.76,"speed":14.49,"timeStep":8.0},{"edge":{"edgeId":"--104","fromNode":{"latitude":"51.404456094285734","longitude":"5.477570348298111","nodeId":"-16"},"lanes":[{"laneId":"--104_0"},{"laneId":"--104_1"}],"toNode":{"latitude":"51.404057790546894","longitude":"5.424575687577547","nodeId":"-14"}},"id":16,"lane":{"laneId":"--104_0"},"position":89.35,"speed":15.59,"timeStep":9.0},{"edge":{"edgeId":"--104","fromNode":{"latitude":"51.404456094285734","longitude":"5.477570348298111","nodeId":"-16"},"lanes":[{"laneId":"--104_0"},{"laneId":"--104_1"}],"toNode":{"latitude":"51.404057790546894","longitude":"5.424575687577547","nodeId":"-14"}},"id":27,"lane":{"laneId":"--104_0"},"position":106.39,"speed":17.03,"timeStep":10.0},{"edge":{"edgeId":"--104","fromNode":{"latitude":"51.404456094285734","longitude":"5.477570348298111","nodeId":"-16"},"lanes":[{"laneId":"--104_0"},{"laneId":"--104_1"}],"toNode":{"latitude":"51.404057790546894","longitude":"5.424575687577547","nodeId":"-14"}},"id":35,"lane":{"laneId":"--104_0"},"position":125.24,"speed":18.85,"timeStep":11.0},{"edge":{"edgeId":"--104","fromNode":{"latitude":"51.404456094285734","longitude":"5.477570348298111","nodeId":"-16"},"lanes":[{"laneId":"--104_0"},{"laneId":"--104_1"}],"toNode":

只是为了让您了解 JSON 的样子。

现在我想将此 JSON 文件的每个项目转换为 Java 对象。所以我想查看“fromNode”内部以获取创建对象的属性。

我可以看到所有的运动和边缘,这没问题(我这样做):

String jsonStringMike = callURL("http://----------/");

try {
JSONArray jsonArrayMike = new JSONArray(jsonStringMike);

int countMike = jsonArrayMike.length(); // get totalCount of all jsonObjects


for (int i = 0; i < countMike; i++) { // iterate through jsonArray
JSONObject jsonObject = jsonArrayMike.getJSONObject(i); // get jsonObject @ i position
JSONArray movement = jsonObject.getJSONArray("movements");
String edge = movement.getJSONObject(i).getString("edge");
System.out.println(edge);

因此,这为我提供了前面显示的 JSON 文件中的所有“边缘”。但如果我想查看“fromNode”,我会收到以下错误:

org.json.JSONException: JSONObject["edge"] is not a JSONArray.

这是不起作用的代码,并将提供给定的错误。

public static void main(String[] args) throws IOException, JSONException {
String jsonStringMike = callURL("http://6--------------");

try {
JSONArray jsonArrayMike = new JSONArray(jsonStringMike);

int countMike = jsonArrayMike.length(); // get totalCount of all jsonObjects


for (int i = 0; i < countMike; i++) { // iterate through jsonArray
JSONObject jsonObject = jsonArrayMike.getJSONObject(i); // get jsonObject @ i position
JSONArray movement = jsonObject.getJSONArray("movements");

int o = movement.length();
String test = movement.toString();
JSONArray jsonArray2 = new JSONArray(test);


for (int p = 0; p < o; o++) {
JSONObject moveObject = jsonArray2.getJSONObject(p);


JSONArray edge = moveObject.getJSONArray("edge");
String fromNode = edge.getJSONObject(p).getString("fromNode");
System.out.println(fromNode);

}

有人知道如何“更深入”地查看给定的 JSON 文件吗?

最佳答案

我想你已经差不多了,你的代码中的问题是循环中的 moveObject 是你的边缘 json 对象,所以 movemen.length() 是数组中所有边缘 json 对象的计数。我还没有测试过这个,但我认为它应该有效。另外,你的 for 循环有拼写错误,o++ 应该是 p++,

     JSONArray jsonArrayMike = new JSONArray(jsonStringMike);

int countMike = jsonArrayMike.length(); // get totalCount of all jsonObjects


for (int i = 0; i < countMike; i++) { // iterate through jsonArray
JSONObject jsonObject = jsonArrayMike.getJSONObject(i); // get jsonObject @ i position
JSONArray movement = jsonObject.getJSONArray("movements");

int o = movement.length();
for (int p = 0; p < o; p++)
{
JSONObject movementObject = movement.getJSONObject(p);
JSONObject edgeObject = movementObject.getJSONObject("edge");

JSONObject fromNode = edgeObject.getJSONObject("fromNode");
String latitude = fromNode.getString("latitude");
String longitude = fromNode.getString("longitude");
String nodeId = fromNode.getString("nodeId");
System.out.println(fromNode);
}
}

关于java - 从 JSON http 请求中读取特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com