使用杰克逊

时间:2018-09-25 09:49:01

标签: java json jackson

嗨,我想将此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" }

我使用http://www.jsonschema2pojo.org创建了4个模型。 这些模型中只有吸气剂设置器。看起来像这样

 @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);
}

现在,我想通过创建它们各自的对象来设置这些模型中的值,最后我得到了一个包含所有数据的主要对象。像这样

  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");

现在 obj 是我要传递给http请求并调用api并从中获取数据的最后一个对象。我想确保正确创建了json,我该如何打印该对象内的所有数据?我对这种方法是否正确?

2 个答案:

答案 0 :(得分:2)

如果使用maven,请将gson放入pom.xml

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

然后像这样打印您的对象

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

您的对象将以json打印

答案 1 :(得分:0)

我找到了两个熟悉您情况的完整示例。

1)使用Gson请参阅教程Parse json string and java object into Gson tree model

2)使用杰克逊,请参考教程Convert Java Object to/from JSON using JACKSON API

希望获得帮助。