【问题标题】:JSON Response to JavaJSON 对 Java 的响应
【发布时间】:2022-01-25 05:12:13
【问题描述】:

我有这个 JSON 响应,我想将其转换为 java。

{"id":"4T4446Pj","count":4,"feasible":true,"route":{"0":{"name":"The Hague, The Netherlands","arrival":0,"distance":0},"1":{"name":"The Hague, The Netherlands","arrival":5,"distance":3.9},"2":{"name":"Uden, The Netherlands","arrival":93,"distance":137.1},"3":{"name":"Sint-Oedenrode, The Netherlands","arrival":112,"distance":160.5}}}

我尝试过在线转换器,但它们总是产生不同的类,其中一个类具有 (0,1,2,3) 的不同属性。

public class Application {
  private String id;
  private float count;
  private boolean feasible;
  Route RouteObject;

 // Getter Methods 
 // Setter Methods 
}

public class Route {
  0 0Object;
  1 1Object;
  2 2Object;
  3 3Object;

 // Getter Methods 
 // Setter Methods 
}

public class 1 {
  private String name;
  private float arrival;
  private float distance;
 // Getter Methods 
 // Setter Methods 
}
public class 0 {
  private String name;
  private float arrival;
  private float distance;
 // Getter Methods 
 // Setter Methods 
} //the same for class 2 and 3

位置的数量不固定,所以我希望位置是一个数组或ArrayList。我该怎么做?

【问题讨论】:

  • 类名不能以数字开头:stackoverflow.com/questions/65475/…
  • 这意味着您将不得不 1) 手动设计/实现 Java 类和 2) 编写自定义映射器。
  • @StephenC 或者只使用Map<String, Route>
  • 是的......也许......但关键是自动 JSON -> Java 类生成器不起作用。

标签: java json api


【解决方案1】:

确保您了解difference between classes and objects。 为了在您的 Java 代码中使用 JSON,您必须

  1. 创建模仿 JSON 结构的 Java 类
  2. 使用像 Jackson 这样的 JSON 解析器从 JSON 创建 Java 对象实例。这也称为“反序列化”或“解组”。

使用Jackson,您可以使用注释(@JsonProperty)来识别从JSON 解析的Java 字段。 您的根对象可能如下所示:

public class Application {
    @JsonProperty // indicate that this field exists in the JSON structure
    private String id;
    @JsonProperty
    private float count;
    @JsonProperty
    private boolean feasible;
    @JsonProperty("route") // indicates that the field name in the JSON is different from the field name in JAVA
    private Map<Integer, Route> routeMap;

    public String getId() {
        return id;
    }

    public float getCount() {
        return count;
    }

    public boolean isFeasible() {
        return feasible;
    }

    public Map<Integer, Route> getRouteMap() {
        return routeMap;
    }
}

表示路由的类可能如下所示:

public class Route {
    @JsonProperty
    private String name;
    @JsonProperty
    private float arrival;
    @JsonProperty
    private float distance;

    public String getName() {
        return name;
    }

    public float getArrival() {
        return arrival;
    }

    public float getDistance() {
        return distance;
    }
}

您现在可以使用 Jackson ObjectMapper 将 JSON 解析为 Java:

// place your JSON in this string 
String json = "{\"id\":\"4T4446Pj\",\"count\":4,...";
// create Jackson JSON <=> Object mapper
ObjectMapper objectMapper = new ObjectMapper();
// parse the json into the object
Application parsedObject = objectMapper.readValue(json, Application.class);

System.out.println("id: " + parsedObject.getId());
System.out.println("count: " + parsedObject.getCount());
System.out.println("feasible: " + parsedObject.isFeasible());
System.out.println("Number of routes: " + parsedObject.getRouteMap().size());
System.out.println("Routes:");
for (Map.Entry<Integer, Route> entry : parsedObject.getRouteMap().entrySet()) {
    Integer index = entry.getKey();
    Route route = entry.getValue();

    System.out.println("\tRoute Index: " + index);
    System.out.println("\tName: " + route.getName());
    System.out.println("\tArrival: " + route.getArrival());
    System.out.println("\tDistance: " + route.getDistance());
    System.out.println("\t----------------------------------------------");
}

此代码将生成输出:

id: 4T4446Pj
count: 4.0
feasible: true
Number of routes: 4
Routes:
    Route Index: 0
    Name: The Hague, The Netherlands
    Arrival: 0.0
    Distance: 0.0
    ----------------------------------------------
    Route Index: 1
    Name: The Hague, The Netherlands
    Arrival: 5.0
    Distance: 3.9
    ----------------------------------------------
    Route Index: 2
    Name: Uden, The Netherlands
    Arrival: 93.0
    Distance: 137.1
    ----------------------------------------------
    Route Index: 3
    Name: Sint-Oedenrode, The Netherlands
    Arrival: 112.0
    Distance: 160.5
    ----------------------------------------------

Jackson 不是 JSON 到 Java 解析的唯一库。但他们都有一个相似的概念。

@JsonProperty 之外还有一个lot of annotations 可以处理诸如忽略字段、自定义解析器或多态类型识别等问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2020-07-02
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多