简体   繁体   中英

Create JsonArray in Jersey with Jackson

I am using Jersey with Jackson to process JSON. At the momenant I am trying warp th result of my query as JSONArray. I would like to get the following Format:

{
  "map_data":
             [
               {"route": 2, "latitude": 89.667781, "longitude": 13.517741},
               {"route": 7, "latitude": 89.657772, "longitude": 13.537742},
               {"route": 9, "latitude": 89.647763, "longitude": 13.547754},
               {"route": 12, "latitude":89.637754, "longitude": 13.567765}
              ]

}

How can this Format by Jackson?

code in the Database class:

    if (busExist.next()) {
        for (int value : selected) {
            PreparedStatement preparedMap = con
                    .prepareStatement("SELECT route, latitude, longitude from bus where route= ?");
            preparedMap.setInt(1, value);
            ResultSet rsMap = preparedMap.executeQuery();
            while (rsMap.next()) {
                int route = rsMap.getInt("route");
                double lat = rsMap.getDouble("latitude");
                double lon = rsMap.getDouble("longitude");

            }

        }

    }

Edit:

    if (busExist.next()) {
        List<MapData> LRLHistory = new ArrayList<>();
        for (int value : selected) {
            PreparedStatement preparedMap = con
                    .prepareStatement("SELECT route, latitude, longitude from bus where route= ?");
            preparedMap.setInt(1, value);
            ResultSet rsMap = preparedMap.executeQuery();
            while (rsMap.next()) {
                int route = rsMap.getInt("route");
                double lat = rsMap.getDouble("latitude");
                double lon = rsMap.getDouble("longitude");

                MapData da_ma = new MapData(route, lat, lon);
                LRLHistory.add(da_ma);


            }

        }
        //System.out.println("The output of LRLHistory : "+LRLHistory);


    }

**Edit 2: **

        Database db = new Database();
        List<MapData> lrl = db.get_map_data(selected);

        try {
            ObjectWriter ow = new ObjectMapper().writer()
                    .withDefaultPrettyPrinter();
            String json = ow.writeValueAsString(lrl);

            System.out.println("The output of json: " + json);
}

Regarding adding the map_data field, you can add a container Java object before serializing it to a json String:

public static class MapDataHolder {
  List<MapData> mapDataList;

  public MapDataHolder(List<MapData> mapData) {
    this.mapDataList = mapData;
  }

  @JsonProperty("map_data") 
  public List<TestStuff.MapData> getMapData() {
    return mapDataList;
  }
}

.

MapDataHolder mapDataHolder = new MapDataHolder(lrl);
String json = ow.writeValueAsString(mapDataHolder);

.

The output of json: {
  "map_data" : [ {
    "route" : 1,
    "lat" : 1.1,
    "lon" : 1.2
  }, {
    "route" : 2,
    "lat" : 2.1,
    "lon" : 2.2
  } ]
}

Of course, you may want to rename accordingly (eg MapDataHolder -> MapData, MapData -> MapDataEntry).

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