简体   繁体   中英

Read specific items from JSON http request

I get JSON from a rest server (jax rs) through a http request.

[{"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":

Just to give you an idea how the JSON looks like.

Now I want to convert every item of this JSON file to an Java object. So I want to see inside the "fromNode" to get the attributes to make an object.

I can see all the movement and edges, thats no problem (i do that like this):

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);

So this provides me with all the "edge's" From the JSON file shown before. But if I want to see the "fromNode" I get the following error:

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

This is the code that is not working and will provide the given error.

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);

            }

Does anybody knows how I can look "deeper" into the given JSON file?

I think you are almost there, the issue in your code that in the loop moveObject is your edge json object, so movemen.length() is the the count of all edge json objects in the array. I have not tested this, but I think it should work. Also, your for loop has typo, o++ should be 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);
    }
  }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM