gpt4 book ai didi

java - 如何在 Java8 中创建 Json 数组并且仅在设置值时打印 json?

转载 作者:行者123 更新时间:2023-11-30 01:57:35 24 4
gpt4 key购买 nike

我正在尝试使用 fasterxml jackson 生成以下内容..但我陷入困境。我似乎不知道如何创建数组。

{
"setAccId":"12345",
"groupOf":null,
"isEnabled":false,
"list":[
{
"student":"jim",
"type":"S_A",
"retro":null
},
{
"student":"bob",
"type":"S_A",
"retro":null
}
],
"sort":[]
}

我有两个类。一个具有 Json 属性,另一个是我打印它的位置。

Below class (DynamicJsonHelper) is where I have all the json properties

package com.company.jsonfc;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({
"accId",
"groupOf",
"isEnabled"
})

public class DynamicJsonHelper {

public String accId;
public String groupOf;
public List studentList;

@JsonProperty("accId")
public void setAccId(String accId) {
this.accId = accId;
}

@JsonProperty("groupOf")
public void setGroupOf(String groupOf) {
this.groupOf = groupOf;
}

@JsonProperty("isEnabled")
public boolean isEnabled() {
return false;
}

@JsonProperty("studentList")
public List<StudentList> studentList() {
return studentList;
}

}

Student List Class (as suggested)

class StudentList {
String student;
String type;
String retro;
}

And here is class (PrintJson) where I call it.

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import com.company.jsonfc.DynamicJsonHelper;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PrintJson {

@Test
public void create_json() throws JsonProcessingException {
final JsonNodeFactory factory = JsonNodeFactory.instance;

DynamicJsonHelper dynamicJsonHelper = new DynamicJsonHelper();
String jsonString;
ObjectMapper mapper = new ObjectMapper();

dynamicJsonHelper.setAccId("12345");

jsonString = mapper.writeValueAsString(dynamicJsonHelper);
System.out.println(jsonString);
}

}

This results in printing the following:

{
"setAccId":"12345",
"groupOf":null,
"isEnabled":false
"studentList":null
}

1) 如何在末尾添加 list:[ ... ] 数组和 sort: [ ] ?

2)在PrintJson类中,我没有为groupOf设置值,但它仍然是在Json中创建的。我如何设置它,以便如果我设置值,它就会被打印..否则它不会包含在正在打印的 json 中。

如果您使用我的代码并为我提供示例以便更好地理解,我将不胜感激

最佳答案

1) How do I add the list:[ ... ] array & the sort: [ ] at the end too?

答案:您可以再创建两个类,第一个类用于 list和一个 sort 。现在上课DynamicJsonHelper您可以添加它们,就像添加 accId 一样或isEnabled并且它们将被打印。确保将所需的字段添加为两个类中的实例变量。例如,对于列表,您可以有一个类,例如:

class StudentList{
String student;
String type;
String retro;
}

现在在您的类中添加一个字段 DynamicJsonHelperList<StudentList> 。同样,您可以对 sort 执行此操作.

2) In class PrintJson, I don't set value for groupOf but it is still created in Json. How do I set it so if I set value, it is printed.. otherwise it is not included in the json being printed.

回答:您可以或者使用对象映射器并将其设置为在序列化期间忽略空字段。例如:mapper.setSerializationInclusion(Include.NON_NULL);或者您可以在类级别将其设置为忽略空值(如果有)。例如:

@JsonInclude(Include.NON_NULL)
class Test
{
String t;
}

正如上面 aBnormaLz 的评论中提到的,如果类型像 isEnabled 那样是原始类型,则不起作用。因此,请考虑将其更改为 boolean 值,并确保其他字段也同样如此。

编辑:

@JsonPropertyOrder({
"accId",
"groupOf",
"isEnabled"
})
@JsonInclude(JsonInclude.Include.NON_NULL)

public class DynamicJsonHelper {

public String accId;
public String groupOf;
public List<Student> studentList;

@JsonProperty("accId")
public void setAccId(String accId) {
this.accId = accId;
}

@JsonProperty("groupOf")
public void setGroupOf(String groupOf) {
this.groupOf = groupOf;
}

@JsonProperty("isEnabled")
public boolean isEnabled() {
return false;
}

@JsonProperty("studentList")
public void setStudentList(List<Student> list) {
this.studentList = list;
}

}

class Student {
private String student;
private String type;
private String retro;

public Student(String student, String type, String retro) {
this.student = student;
this.type = type;
this.retro = retro;
}

public String getStudent() {
return student;
}

public void setStudent(String student) {
this.student = student;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getRetro() {
return retro;
}

public void setRetro(String retro) {
this.retro = retro;
}
}

class HelperTest{

public static void main(String[] args) throws JsonProcessingException {
DynamicJsonHelper dynamicJsonHelper = new DynamicJsonHelper();
String jsonString;
ObjectMapper mapper = new ObjectMapper();

dynamicJsonHelper.setAccId("12345");

List<Student> list = Arrays.asList(new Student("s1", "t1", "r1"), new Student("s2", "t2", "r2"));

dynamicJsonHelper.setStudentList(list);
jsonString = mapper.writeValueAsString(dynamicJsonHelper);
System.out.println(jsonString);

}
}

执行程序后output如下图:

{
"accId": "12345",
"isEnabled": false,
"studentList": [
{
"student": "s1",
"type": "t1",
"retro": "r1"
},
{
"student": "s2",
"type": "t2",
"retro": "r2"
}
]
}

关于java - 如何在 Java8 中创建 Json 数组并且仅在设置值时打印 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53855699/

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