为帖子请求创建一个json [英] Creating a json for a post request
问题描述
我正在尝试将JSON字符串发送到post方法以便接收航班信息,我有以下json示例我必须修改以提出不同的请求
I am trying to send JSON string to post method in order to receive flights information, i have the following json example i must modify to make different requests
{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "YYYY-MM-DD"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "YYYY-MM-DD"
}
]
}
}
我有以下课程
public class Request {
public class Passengers{
private int adultCount;
public int getAdultCount() {
return adultCount;
}
public void setAdultCount(int adultCount) {
this.adultCount = adultCount;
}
}
private List<Slice> slice;
public List<Slice> getSlice() {
return slice;
}
public void setSlice(List<Slice> slice) {
this.slice = slice;
}
public static class Slice{
private String origin;
private String destination;
private String date;
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
}
我如何映射此类设置值并使用相同的格式创建不同的json?我正在尝试杰克逊,但我没有得到合适的输出
how do i map this class to set the values and create a different json with the same format? i am trying with Jackson but im getting no suitable output
推荐答案
这是JSON格式。了解它。
JSON字符串映射到Java String
。 JSON编号映射到Java Number
类型。 JSON数组映射到集合
类型或数组类型。 JSON对象映射到Java Map
类型或任何其他Java类型(之前未提及)。
A JSON string maps to a Java String
. A JSON number maps to a Java Number
type. A JSON array maps to a Collection
type or an array type. A JSON object maps to a Java Map
type or any other Java type (not mentioned previously).
所以Java类如
class Foo {
String name;
int[] array;
Map<String, List<Double>> ref;
}
映射到
{
"name" : "string",
"array" : [1, 2, 3],
"ref" : {
"val1" : [1.2, 3.4]
}
}
您的JSON是根目录下的JSON对象。它包含
Your JSON is a JSON object at the root. It contains
"request": {
这是一个键值对,其中键是JSON字符串,值是JSON对象。
which is a key value pair where the key is a JSON string and the value is a JSON object.
这应该告诉你您需要一个带有名为 request
的字段的Java对象,该字段应该引用另一个对象。递归地执行此分析,您将获得Java对象格式。
This should tell you that you need a Java object with a field named request
which should be referencing another object. Do this analysis recursively and you will get your Java Object formats.
这篇关于为帖子请求创建一个json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!