我使用jackson API将Java转换为JSON,反之亦然。在大型商务中,有一个用于创建货件的API https://developer.bigcommerce.com/api/stores/v2/orders/shipments#create-a-shipment 我只有来自用户的order_id。那么如何使用POJO创建货件?
答案 0 :(得分:3)
以下是一些可以帮助您入门的代码。
public class Shipment {
@JsonProperty("id")
public long id;
@JsonProperty("order_id")
public long orderId;
@JsonProperty("date_created")
public String dateCreated;
@JsonProperty("customer_id")
public long customerId;
@JsonProperty("billing_address")
public Address billingAddress;
@JsonProperty("shipping_address")
public Address shippingAddress;
}
public class Address {
@JsonProperty("zip")
public String zip;
@JsonProperty("city")
public String city;
@JsonProperty("email")
public String email;
@JsonProperty("phone")
public String phone;
@JsonProperty("state")
public String state;
@JsonProperty("company")
public String company;
@JsonProperty("country")
public String country;
@JsonProperty("street_1")
public String street1;
@JsonProperty("street_2")
public String street2;
@JsonProperty("last_name")
public String lastName;
@JsonProperty("first_name")
public String firstName;
@JsonProperty("country_iso2")
public String countryIso2;
}
public class Application {
private static final ObjectMapper objectMapper = new ObjectMapper();
public void doSomething() {
...
// deserialize from JSON string
Shipment shipment = objectMapper.readValue(someJsonString, Shipment);
...
// serialize to JSON string
String someJsonString = objectMapper.writeValueAsString(shipment);
...
}
}