gpt4 book ai didi

java - 使用 Jackson 将 json 转为 java 对象

转载 作者:行者123 更新时间:2023-11-30 02:02:10 25 4
gpt4 key购买 nike

嗨,我想将此 json 转换为 java 中的 json 对象,以便我可以将其传递给 http 请求来调用 api

{
“别名命名”:正确,
"dataServiceType": "浏览",
"设备名称": "我的设备",
"langPref": "",
"最大页面大小": "2000",
“输出类型”:“版本1”,
"密码": "!jshjhsdhshdj",
“询问”: {
“自动清除”:真,
“自动查找”:正确,
“健康)状况”: [
{
"controlId": "F4211.CO",
“运算符”:“等于”,
“值(value)”: [
{
“内容”:“00098”,
“specialValueId”:“文字”
}
]
},
{
"controlId": "F4211.DCTO",
“运算符”:“等于”,
“值(value)”: [
{
“内容”:“SM”,
“specialValueId”:“文字”
}
]
},
{
"controlId": "F4211.UPMJ",
“运算符”:“GREATER_EQUAL”,
“值(value)”: [
{
“内容”:“2017 年 1 月 1 日”,
“specialValueId”:“文字”
}
]
}
],
“匹配类型”:“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",
"目标名称": "F4211",
“目标类型”:“表”,
“ token ”:“044biPNadxNVGhyAKdrImoniK98OOa2l86ZA63qCr4gE5o = MDIwMDA4LTIyNDU5MjUxMTY2MzY3NTA3MTRNeURldmljZTE1Mzc0MjYwMjAyNTk =”,
“用户名”:“阿里”
}

我使用 http://www.jsonschema2pojo.org 创建了 4 个模型。这些模型只是有 getter setter。看起来像这样

 @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 创建正确,我该如何打印该对象内的所有数据?我的这种方法正确吗?

最佳答案

如果你使用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 格式打印

关于java - 使用 Jackson 将 json 转为 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com