从JSON http请求中读取特定项

时间:2015-05-24 14:28:21

标签: java json

我通过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);

因此,这为我提供了所有&#34;边缘&#34;从之前显示的JSON文件。但是,如果我想看到&#34; fromNode&#34;我收到以下错误:

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

            }

有谁知道我的外表和更深层次的&#34;进入给定的JSON文件?

1 个答案:

答案 0 :(得分:1)

我认为你几乎就在那里,你的代码中的问题是循环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);
    }
  }