如何使用servlet使用JSONObject在Java中创建正确的JsonArray

时间:2016-06-14 08:18:39

标签: java arrays json servlets

如何使用JSONObject在Java中创建如下所示的JSON对象?

    [
  {
    "title": "ZAKI",
    "start": "2010-05-01",
    "placemarks": [
      {
        "polyline": [
          {
            "lat": 48.22467264956519,
            "lon": 16.32568359375
          },
          {
            "lat": 47.517200697839414,
            "lon": 18.984375
          },
          {
            "lat": 45.84410779560204,
            "lon": 15.9521484375
          },
          {
            "lat": 48.22467264956519,
            "lon": 16.32568359375
          }
        ]
      },
      {
        "point": {
          "lat": 48.22467264956519,
          "lon": 16.32568359375
        }
      },
      {
        "point": {
          "lat": 47.517200697839414,
          "lon": 18.984375
        }
      },
      {
        "point": {
          "lat": 45.84410779560204,
          "lon": 15.9521484375
        }
      }
    ]
  }
]

我正在使用servlet。

这是我的servlet代码

JSONArray arrayObj = new JSONArray();
            for (int i = 0; i < listDataTracking.size(); i++) {
                JSONObject obj = new JSONObject();
                EntityTracking dataTracking = listDataTracking.get(i);

                if (dataTracking.getIdTracking() == null) {
                    obj.put("Id", "");
                } else {
                    obj.put("Id", dataTracking.getIdTracking());
                    obj.put("title", dataTracking.getUser_name().getUserName().toUpperCase());
                    obj.put("start", sdf.format(dataTracking.getTglSend()));
//                                       
                    String lat = "";
                    String lon = "";
                    String jamSend = "";
                    Long id = 0l;
//                    

                    String queryStmnt_geoTracking = "SELECT entityGeoTracking.id FROM EntityGeoTracking entityGeoTracking "
                            + "WHERE entityGeoTracking.idTracking.idTracking=\"" + dataTracking.getIdTracking() + "\"";
                    javax.persistence.Query query_geoTracking = em.createQuery(queryStmnt_geoTracking);
                    System.out.println("Isi queryStmnt_geoTracking" + queryStmnt_geoTracking);
                    List<EntityGeoTracking> cekid = query_geoTracking.getResultList();
                    if (cekid.isEmpty()) {
                        System.out.println("Isi cekid == null");
                        return;
                    }
                    System.out.println("Isi cekid" + cekid);
                    EntityGeoTracking dataGeoTraking = em.find(EntityGeoTracking.class, cekid);
                    if (lat.isEmpty()) {

                        lat = dataGeoTraking.getGetLatitude();
                    }
                    if (lon.isEmpty()) {
                        lon = dataGeoTraking.getGetLongitude();
                    }
                    if (id == 0l) {
                        id = dataGeoTraking.getId();
                    }
                    if (jamSend.isEmpty()) {
                        jamSend = dataGeoTraking.getJamSend();
                    }
                    System.out.println("chek value lat, lon, id, jamSend" + lat + "" + lon + "" + id + "" + jamSend);

                   javax.json.JsonArray value = Json.createArrayBuilder()
                            .add(Json.createObjectBuilder()
                            .add("polyline", Json.createObjectBuilder()
                            .add("lat", lat)
                            .add("lon", lon)))
                            .add(Json.createObjectBuilder()
                            .add("point", Json.createObjectBuilder()
                            .add("lat", lat)
                            .add("lon", lon)))
                           .build();


                    obj.put("placemarks", value);

                }



                arrayObj.add(obj);
            }

            em.close();

            JSONObject rows = new JSONObject();

//            rows.put("results", arrayObj);//results
//            rows.put("results", listDataTracking.size());//results
//            rows.put("rows", arrayObj);
//            writer.print(rows.toString());
//            rows.put("", listDataTracking.size());//results
            rows.put("", arrayObj);
            System.out.println(" List : " + arrayObj.toString());
            writer.print(arrayObj);
            writer.close();

//            }
//            br.close();

        } catch (Exception ex) {
            ex.getMessage();
            System.out.println("ERROR: " + ex.getMessage());
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }

我的outPut JSON格式

   [
  {
    "Id": 1,
    "title": "ZAKI",
    "start": "2016-06-15",
    "placemarks": [
      {
        "polyline": {
          "lat": {
            "chars": "-6.2206080",
            "string": "-6.2206080",
            "valueType": "STRING"
          },
          "lon": {
            "chars": "106.7810640",
            "string": "106.7810640",
            "valueType": "STRING"
          }
        }
      },
      {
        "point": {
          "lat": {
            "chars": "-6.2206080",
            "string": "-6.2206080",
            "valueType": "STRING"
          },
          "lon": {
            "chars": "106.7810640",
            "string": "106.7810640",
            "valueType": "STRING"
          }
        }
      }
    ]
  }
]

我已经多次尝试但是我没有成功。请帮忙!

1 个答案:

答案 0 :(得分:0)

{
    "title": "ZAKI",
    "start": "2010-05-01",
    "placemarks": [
        {
            "polyline": [
                {
                    "lat": 48.2246726495652,
                    "lon": 16.32568359375
                },
                {
                    "lat": 47.5172006978394,
                    "lon": 18.984375
                },
                {
                    "lat": 45.844107795602,
                    "lon": 15.9521484375
                },
                {
                    "lat": 48.2246726495652,
                    "lon": 16.32568359375
                }
            ]
        },
        {
            "point": {
                "lat": 48.2246726495652,
                "lon": 16.32568359375
            }
        },
        {
            "point": {
                "lat": 47.5172006978394,
                "lon": 18.984375
            }
        },
        {
            "point": {
                "lat": 45.844107795602,
                "lon": 15.9521484375
            }
        }
    ]
}

首先使用some formatting tool格式化您的json,以便您可以轻松了解预期输出与实际输出之间的差异。

对于折线对象,你应该有一个数组而不是对象中的对象。

Json.createObjectBuilder().add("polyline", 
                              Json.createObjectBuilder().add("lat", lat).add("lon", lon))
                              )