gpt4 book ai didi

java - 如何用java程序制作层次结构的Json结构

转载 作者:行者123 更新时间:2023-11-29 05:32:52 25 4
gpt4 key购买 nike

我正在研究生成 JSON。期望的结果是:

{
"nodes": [
{
"name": "Jeet123",
"type": 1,
"slug": "",
"entity": "company"
}
],
"links": [
{
"source": 0,
"target": 1,
"value": 1,
"distance": 5
}
]
}

我必须制作这个 JSON。我正在编写这段 java 代码..但它只显示{}

条目 1:

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

class Entry1 {

private String name;
private int type;
private String slug;
private String entity;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getType() {
return type;
}

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

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public String getEntity() {
return entity;
}

public void setEntity(String entity) {
this.entity = entity;
}
}

条目 2:

class Entry2 {

private String source;
private String target;
private String value;
private String distance;

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getDistance() {
return distance;
}

public void setDistance(String distance) {
this.distance = distance;
}
}

EntryListContainer:

class EntryListContainer {

public List<Entry1> nodes;
public List<Entry2> links;

public void setEntryList1(List<Entry1> entryList1) {
this.nodes = nodes;
}

public List<Entry1> getEntryList1() {

return nodes;
}

public void setEntryList2(List<Entry2> entryList1) {
this.links = links;
}

public List<Entry2> getEntryList2() {

return links;
}

}

LinkJson:

public class LinkJson {

public static void main(String[] args) {

EntryListContainer container = new EntryListContainer();

List<Entry1> entryList1 = new ArrayList<>();
List<Entry2> entryList2 = new ArrayList<>();

Entry1 entry1 = new Entry1();

entry1.setName("Jeet123");
entry1.setType(1);
entry1.setSlug("");
entry1.setEntity("company");

entryList1.add(entry1);

Entry2 entry2 = new Entry2();

entry2.setSource("0");
entry2.setTarget("1");
entry2.setValue("1");
entry2.setDistance("5");

entryList2.add(entry2);

container.setEntryList1(entryList1);
container.setEntryList2(entryList2);

Gson gson = new Gson();

System.out.println(gson.toJson(container));

}

}

最佳答案

错误的复制/粘贴!

public void setEntryList2(List<Entry2> entryList1) {
this.links = links;
}

public void setEntryList1(List<Entry1> entryList1) {
this.nodes = nodes;
}

你应该有这个:

public void setEntryList2(List<Entry2> links) {
this.links = links;
}

public void setEntryList1(List<Entry1> nodes) {
this.nodes = nodes;
}

关于java - 如何用java程序制作层次结构的Json结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20517642/

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