java - 使用java bean转换json格式

标签 java json

我有一个类似的 json 字符串

{"results":
[{"_type":"Position","_id":377078,"name":"Potsdam, Germany","type":"location","geo_position":{"latitude":52.39886,"longitude":13.06566}},
{"_type":"Position","_id":410978,"name":"Potsdam, USA","type":"location","geo_position":{"latitude":44.66978,"longitude":-74.98131}}]}

我正在尝试转换为

    {"results":
 [{"_type":"Position","_id":377078,"name":"Potsdam, Germany","type":"location","latitude":52.39886,"longitude":13.06566}, 
{"_type":"Position","_id":410978,"name":"Potsdam, USA","type":"location","latitude":44.66978,"longitude":-74.98131}]}

我正在转换为 java 并再次使用但我在数据中获取 null

  SourceJSON    data=new Gson().fromJson(jsonArray, SourceJSON.class);
           DestinationJSON destdata = new DestinationJSON();
           destdata.setLatitide(data.getGeoLocation().getLatitide());
           destdata.setLongitude(data.getGeoLocation().getLongitude());
           destdata.setId(data.getId());
           destdata.setType(data.getType());
           destdata.setName(data.getName());
           destdata.set_type(data.get_type());


           Gson gson = new Gson();
           String json = gson.toJson(destdata);

下面是我的 bean

public class SourceJSON implements Serializable {
    private List<GEOLocation> geoLocations;
    private String  _type;
    private String id;
    private String name;
    private String type;


    public String get_type() {
        return _type;
    }
    public List<GEOLocation> getGeoLocations() {
        return geoLocations;
    }
    public void setGeoLocations(List<GEOLocation> geoLocations) {
        this.geoLocations = geoLocations;
    }
    public void set_type(String _type) {
        this._type = _type;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }



}

public class GEOLocation implements Serializable{
    private String latitide;
    private String longitude;
    public String getLatitide() {
        return latitide;
    }
    public void setLatitide(String latitide) {
        this.latitide = latitide;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }


}

和目标java

public class DestinationJSON  implements Serializable {

    private String  _type;
    private String id;
    private String name;
    private String type;
    private String latitide;
    private String longitude;
    public String get_type() {
        return _type;
    }
    public void set_type(String _type) {
        this._type = _type;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getLatitide() {
        return latitide;
    }
    public void setLatitide(String latitide) {
        this.latitide = latitide;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}

最佳答案

你所需要的就是这个。您可以通过简单的复制和粘贴在 IDE 中尝试此类。

package stackoverflow.questions;

import java.util.*;

import com.google.gson.Gson;

public class Q20433539{

   public static void main(String[] args){
         String json = "{\"results\":"+
         "[{\"_type\":\"Position\",\"_id\":377078,\"name\":\"Potsdam, Germany\",\"type\":\"location\",\"geo_position\":{\"latitude\":52.39886,\"longitude\":13.06566}},"+
         "{\"_type\":\"Position\",\"_id\":410978,\"name\":\"Potsdam, USA\",\"type\":\"location\",\"geo_position\":{\"latitude\":44.66978,\"longitude\":-74.98131}}]}";


   Gson gson = new Gson();
   Map m = gson.fromJson(json, Map.class);

   List<Map> innerList = (List<Map>) m.get("results");
   for(Map result: innerList){
      Map<String, Double> geo_position = (Map<String, Double>) result.get("geo_position");
      result.put("latitude", geo_position.get("latitude"));
      result.put("longitude", geo_position.get("longitude"));
      result.remove("geo_position");
   }
   System.out.println(gson.toJson(m));


   }


}

当然,它的工作原理是假设您始终希望平坦化地理信息。

说明:在使用Gson时使用POJO很方便,但这不是唯一的方法。如果您不指定预期结果,Gson 还可以反序列化为数组/映射。所以我这样做了,然后我操纵结构来展开你的数据。之后,Gson 可以再次将 Arrays/Maps 结构序列化为您想要的 JSON。

关于java - 使用java bean转换json格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20433539/

相关文章:

java - 在 Byte Buddy 中缓存生成的类?

java - WCF 和 Java 互操作性

java - 用 Java 编写通用代码

javascript 获取 json 内部值

json - Spring 3.2 Jackson 2.2 REST API

javascript - 具有不同 json 变量的循环显示所有项目而不是每个项目一个

java - 使用 spring 数据 elasticsearch 的特定字段的文档、最小值和最大值

java - 如何从 selenium webdriver 的子菜单中选择下拉菜单

java - 无法解析 JSONException Android Studio

html - 根据使用 json 选择的语言更改 html 页面的内容