[英]How to make a Hirarchical Json Structure with java program
I am working on a generation JSON. 我正在研究一代JSON。 The desired result is:
期望的结果是:
{
"nodes": [
{
"name": "Jeet123",
"type": 1,
"slug": "",
"entity": "company"
}
],
"links": [
{
"source": 0,
"target": 1,
"value": 1,
"distance": 5
}
]
}
This JSON i have to make. 这个JSON我必须做。 I am Writing this java code.. But it only shows {}
我正在写这个java代码..但它只显示{}
Entry1
: Entry1
:
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;
}
}
Entry2
: Entry2
:
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
: 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
: 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));
}
}
Create Constructor for your EntryListContainer
class: 为
EntryListContainer
类创建构造函数:
class EntryListContainer {
private List<Entry1> nodes;
private List<Entry2> links;
public EntryListContainer(List<Entry1> nodes, List<Entry2> links) {
this.nodes = nodes;
this.links = links;
}
}
And then create the container
object this way: 然后以这种方式创建
container
对象:
EntryListContainer container = new EntryListContainer(entryList1,entryList2);
And then create the json: 然后创建json:
Gson gson = new Gson();
System.out.println(gson.toJson(container));
Edit : Not necessary to use the constructor, 编辑 :没有必要使用构造函数,
Change the following methods in your EntryListContainer
class and it will work: 在
EntryListContainer
类中更改以下方法,它将起作用:
public void setEntryList1(List<Entry1> entryList1) {
this.nodes = entryList1;
}
public void setEntryList2(List<Entry2> entryList2) {
this.links = entryList2;
}
Bad copy/paste ! 不好复制/粘贴!
public void setEntryList2(List<Entry2> entryList1) {
this.links = links;
}
public void setEntryList1(List<Entry1> entryList1) {
this.nodes = nodes;
}
You should have this : 你应该这样:
public void setEntryList2(List<Entry2> links) {
this.links = links;
}
public void setEntryList1(List<Entry1> nodes) {
this.nodes = nodes;
}
The way JSON is usually parsed to get the properties is to find the property names ( nodes
and links
in your code) and then capitalise the first letter and append the get
word onto the front to try and use the getter method. 通常解析JSON以获取属性的方法是查找属性名称(代码中的
nodes
和links
),然后将第一个字母大写并将get
字词附加到前面以尝试使用getter方法。 Basically your EntryListContainer
doesn't follow the Java Bean Conventions that JSON (and GSON by proxy) relies on. 基本上,您的
EntryListContainer
不遵循JSON(和代理的GSON)所依赖的Java Bean约定 。 So it didn't print anything because you had no getter method for getNodes
or getLinks
, you have getEntryList1
and getEntryList2
因此,它没有打印任何东西,因为你有没有为getter方法
getNodes
或getLinks
,你有getEntryList1
和getEntryList2
I'm pretty sure you EntryListContainer class needs to look like this: 我很确定你的EntryListContainer类需要看起来像这样:
class EntryListContainer {
public List<Node> nodes;
public List<Link> links;
public void setNodes(final List<Node> nodes) {
this.nodes = nodes;
}
public List<Node> getNodes() {
return this.nodes;
}
public void setLinks(final List<Link> links) {
this.links = links;
}
public List<Link> getLinks() {
return this.links;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.