简体   繁体   English

使用杰克逊将json转换为java对象

[英]json to java Object using jackson

Hi i want to convert this json to json object in java so that i can pass it to http request to call an api 嗨,我想将此json转换为java中的json对象,以便我可以将其传递给http请求以调用api

{ "aliasNaming": true, "dataServiceType": "BROWSE", "deviceName": "MyDevice", "langPref": " ", "maxPageSize": "2000", "outputType": "VERSION1", "password": "!jshjhsdhshdj", "query": { "autoClear": true, "autoFind": true, "condition": [ { "controlId": "F4211.CO", "operator": "EQUAL", "value": [ { "content": "00098", "specialValueId": "LITERAL" } ] }, { "controlId": "F4211.DCTO", "operator": "EQUAL", "value": [ { "content": "SM", "specialValueId": "LITERAL" } ] }, { "controlId": "F4211.UPMJ", "operator": "GREATER_EQUAL", "value": [ { "content": "01/01/17", "specialValueId": "LITERAL" } ] } ], "matchType": "MATCH_ALL" }, "returnControlIDs": "F4211.DOCO|F4211.TRDJ|F4211.CRCD|F4211.AN8|F4211.DSC2|F4211.DSC1|F4211.LITM|F4211.LOTN|F4211.UORG|F4211.UPRC|F4211.AEXP", "targetName": "F4211", "targetType": "table", "token": "044biPNadxNVGhyAKdrImoniK98OOa2l86ZA63qCr4gE5o=MDIwMDA4LTIyNDU5MjUxMTY2MzY3NTA3MTRNeURldmljZTE1Mzc0MjYwMjAyNTk=", "username": "Ali" }

i have created 4 models using http://www.jsonschema2pojo.org . 我使用http://www.jsonschema2pojo.org创建了4个模型。 those models just have getter setter in it. 这些模型中只有吸气剂设置器。 look something like this 看起来像这样

 @JsonProperty("aliasNaming")
private Boolean aliasNaming;
@JsonProperty("dataServiceType")
private String dataServiceType;
@JsonProperty("deviceName")
private String deviceName;
@JsonProperty("langPref")
private String langPref;
@JsonProperty("maxPageSize")
private String maxPageSize;
@JsonProperty("outputType")
private String outputType;
@JsonProperty("password")
private String password;
@JsonProperty("query")
private Query query;
@JsonProperty("returnControlIDs")
private String returnControlIDs;
@JsonProperty("targetName")
private String targetName;
@JsonProperty("targetType")
private String targetType;
@JsonProperty("token")
private String token;
@JsonProperty("username")
private String username;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("aliasNaming")
public Boolean getAliasNaming() {
    return aliasNaming;
}

@JsonProperty("aliasNaming")
public void setAliasNaming(Boolean aliasNaming) {
    this.aliasNaming = aliasNaming;
}

@JsonProperty("dataServiceType")
public String getDataServiceType() {
    return dataServiceType;
}

@JsonProperty("dataServiceType")
public void setDataServiceType(String dataServiceType) {
    this.dataServiceType = dataServiceType;
}

@JsonProperty("deviceName")
public String getDeviceName() {
    return deviceName;
}

@JsonProperty("deviceName")
public void setDeviceName(String deviceName) {
    this.deviceName = deviceName;
}

@JsonProperty("langPref")
public String getLangPref() {
    return langPref;
}

@JsonProperty("langPref")
public void setLangPref(String langPref) {
    this.langPref = langPref;
}

@JsonProperty("maxPageSize")
public String getMaxPageSize() {
    return maxPageSize;
}

@JsonProperty("maxPageSize")
public void setMaxPageSize(String maxPageSize) {
    this.maxPageSize = maxPageSize;
}

@JsonProperty("outputType")
public String getOutputType() {
    return outputType;
}

@JsonProperty("outputType")
public void setOutputType(String outputType) {
    this.outputType = outputType;
}

@JsonProperty("password")
public String getPassword() {
    return password;
}

@JsonProperty("password")
public void setPassword(String password) {
    this.password = password;
}

@JsonProperty("query")
public Query getQuery() {
    return query;
}

@JsonProperty("query")
public void setQuery(Query query) {
    this.query = query;
}

@JsonProperty("returnControlIDs")
public String getReturnControlIDs() {
    return returnControlIDs;
}

@JsonProperty("returnControlIDs")
public void setReturnControlIDs(String returnControlIDs) {
    this.returnControlIDs = returnControlIDs;
}

@JsonProperty("targetName")
public String getTargetName() {
    return targetName;
}

@JsonProperty("targetName")
public void setTargetName(String targetName) {
    this.targetName = targetName;
}

@JsonProperty("targetType")
public String getTargetType() {
    return targetType;
}

@JsonProperty("targetType")
public void setTargetType(String targetType) {
    this.targetType = targetType;
}

@JsonProperty("token")
public String getToken() {
    return token;
}

@JsonProperty("token")
public void setToken(String token) {
    this.token = token;
}

@JsonProperty("username")
public String getUsername() {
    return username;
}

@JsonProperty("username")
public void setUsername(String username) {
    this.username = username;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

Now i want to set the values in these models by creating their respective objects and finally i got one main object with all the data. 现在,我想通过创建它们各自的对象来设置这些模型中的值,最后我得到了一个包含所有数据的主对象。 like this 像这样

  Value Vobj1 = new Value();
  Vobj1.setContent("00098");
  Vobj1.setSpecialValueId("LITERAL");


  List<Value> valueList1= new ArrayList<Value>();
  valueList1.add(Vobj1);

  Value Vobj2 = new Value();
  Vobj2.setContent("SM");
  Vobj2.setSpecialValueId("LITERAL");

  List<Value> valueList2= new ArrayList<Value>();
  valueList2.add(Vobj2);

  Value Vobj3 = new Value();
  Vobj3.setContent("01/01/17");
  Vobj3.setSpecialValueId("LITERAL");

  List<Value> valueList3= new ArrayList<Value>();
  valueList3.add(Vobj3);

  Condition Cobj1 = new Condition();
  Cobj1.setControlId("F4211.CO");
  Cobj1.setOperator("EQUAL");
  Cobj1.setValue(valueList1);

  Condition Cobj2 = new Condition();
  Cobj2.setControlId("F4211.DCTO");
  Cobj2.setOperator("EQUAL");
  Cobj2.setValue(valueList1);

  Condition Cobj3 = new Condition();
  Cobj3.setControlId("F4211.UPMJ");
  Cobj3.setOperator("GREATER_EQUAL");
  Cobj3.setValue(valueList1);

  List<Condition> conditionList1 = new ArrayList<Condition>();
  conditionList1.add(Cobj1);
  conditionList1.add(Cobj2);
  conditionList1.add(Cobj3);


  Query Qobj1= new Query();
  Qobj1.setAutoClear(true);
  Qobj1.setAutoFind(true);
  Qobj1.setCondition(conditionList1);
  Qobj1.setMatchType("MATCH_ALL");

  JSONStructure obj=new JSONStructure();
  obj.setAliasNaming(true);
  obj.setDataServiceType("BROWSE");
  obj.setDeviceName("MyDevice");
  obj.setLangPref("  ");
  obj.setMaxPageSize("2000");
  obj.setOutputType("VERSION1");
  obj.setPassword("!J0g3t6000");
  obj.setQuery(Qobj1);
  obj.setReturnControlIDs("F4211.DOCO|F4211.TRDJ|F4211.CRCD|F4211.AN8|F4211.DSC2|F4211.DSC1|F4211.LITM|F4211.LOTN|F4211.UORG|F4211.UPRC|F4211.AEXP");
  obj.setTargetName("F4211");
  obj.setTargetType("table");
  obj.setToken(Token);
  obj.setUsername("JOGET");

Now obj is my final object that i am going to pass to an http request and call the api and get the data from it. 现在obj是我要传递给http请求并调用api并从中获取数据的最终对象。 i want to make sure that my json is created correct, how am i suppose to print the all the data inside this object? 我想确保正确创建了json,我该如何打印该对象内的所有数据? and am i going correct with this approach? 我用这种方法正确吗?

if you use maven put gson into your pom.xml 如果使用maven,请将gson放入pom.xml

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.5</version>
</dependency>

then print your object like this 然后像这样打印对象

System.out.println(new Gson().toJson(yourObj));

your object will print in json 您的对象将以json打印

I found two full working examples that is familiar with your case. 我找到了两个熟悉您的案例的完整工作示例。

1) Using Gson refer to the tutorial Parse json string and java object into Gson tree model 1)使用Gson参考教程将json字符串和java对象解析为Gson树模型

2) Using Jackson refer to the tutorial Convert Java Object to/from JSON using JACKSON API 2)使用Jackson指的是使用JACKSON API将Java对象与JSON相互转换的教程

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM