Java Jackson Json:如何将数组转换为 JsonNode 和 ObjectNode?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22978025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:42:59  来源:igfitidea点击:

Hymanson Json: how to convert array to JsonNode and ObjectNode?

javajavascriptjsonHymanson

提问by Stupidfrog

Given Employee and company class

给定 Employee 和 company 类

Company
{
    String companyName;
}

Employee
{
    String employeeName;
}

and my code like the following

和我的代码如下

List<Employee> e = new ArrayList<Employee>();
.....
.....

i wish i can get result like this

我希望我能得到这样的结果

{
    "company":{ 
                "companyName":"cName",
                "Employee":[
                    {"employeeName":"myName1"},
                    {"employeeName":"myName2"},
                    {"employeeName":"myName3"}
                ]
              }
}

It is a simple question, however i quite confusing somethings.... Especially Gson And Json....

这是一个简单的问题,但是我很困惑……尤其是 Gson 和 Json ……

Please do not propose other library, which i COMPULSORY NEEDthis library to work. And due to some circumstance,this class IS NOTi wanted.

请不要提出其他图书馆,我强制NEED这个库的工作。由于某些情况,这门课不是我想要的。

Company
{
    String companyName;
    List<Employee> employees;
}

Therefore i need MANUALLYput it,and serialize to json string.

因此我需要手动放置它,并序列化为 json 字符串。

EDITED:@Sotirios Delimanolis classes declared is the right way to design the relationship between classes. However, that's not i wanted.

编辑:@Sotirios Delimanolis 声明的类是设计类之间关系的正确方法。然而,这不是我想要的。

The Answer from @hsluo is Correct! and same as what @Sotirios Delimanolis mention. Totally fulfill this question.

@hsluo 的回答是正确的!和@Sotirios Delimanolis 提到的一样。完全满足这个问题。

And i did found another way to do it which using Hashmap

我确实找到了另一种使用 Hashmap 的方法

HashMap k = new HashMap();
List<employee> y = new ArrayList<employee>();
y......
k.put("records", y);
k.put("total", total);

and then return to @Responbody, result totally same as @hsluo answered.

然后返回@Responbody,结果与@hsluo 回答完全相同。

AND thanks @Sotirios Delimanolis, @hsluo to help me.

并感谢@Sotirios Delimanolis、@hsluo 帮助我。

采纳答案by hsluo

ObjectMapper mapper = new ObjectMapper();
List<Employee> e = new ArrayList<Employee>();
ArrayNode array = mapper.valueToTree(e);
ObjectNode companyNode = mapper.valueToTree(company);
companyNode.putArray("Employee").addAll(array);
JsonNode result = mapper.createObjectNode().set("company", companyNode);

回答by Sotirios Delimanolis

You can just create a POJO that contains a List<Employee>

您可以只创建一个包含 List<Employee>

class Employees {
    @JsonProperty("Employee")
    private List<Employee> employees;

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

and serialize it to an ObjectNode.

并将其序列化为ObjectNode.

Employees e = new Employees();
List<Employee> employees = new ArrayList<Employee>();
e.setEmployees(employees);
ObjectNode objectNode = mapper.valueToTree(e);

回答by transang

You can use Hymanson ObjectNode's putand putArraylike

您可以使用Hyman逊ObjectNodeputputArray

import com.fasterxml.Hymanson.databind.node.ObjectNode;
ArrayList<ObjectNode> employee = new ArrayList();
for(int i = 1; i < 4; i++){
  ObjectNode em = Json.newObject();
  em.put("companyName", "cName" + i);
  employee.add(em);
}
ObjectNode company = Json.newObject();
company.put("companyName", "cName");
company.putArray("employee").addAll(employee);
return Json.newObject().put("company", company);

回答by Daniel Alonso

JsonNode result = new ObjectMapper().readTree(e.toString());