2013-12-11 66 views
1

我正在研究一代JSON。期望的結果是:如何用java程序製作一個具有層次結構的JSON結構

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

這是我必須做的JSON。我寫這java代碼..但它只能說明{}

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

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)); 

    } 

} 

回答

1

壞複製/粘貼!

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; 
} 
1

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; 
    } 
} 

,然後創建container對象是這樣的:

EntryListContainer container = new EntryListContainer(entryList1,entryList2); 

,然後創建JSON:

Gson gson = new Gson(); 

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

編輯:沒有必要使用構造函數,

改變你EntryListContainer類下面的方法,它會工作:

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

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

爲什麼使用不同於setters的構造函數? – 2013-12-11 11:26:12

+1

@LutzHorn沒有區別,檢查編輯 – Jhanvi

+0

@Jhanvi如果我在你的答案正文的代碼中糾正錯別字,你會不高興嗎?在這兩個setter方法中,參數名稱與實際的參數不匹配,這意味着setter實際上什麼都不做。 –

1

JSON通常被解析,以獲得性能的方法是找物業名稱(在你的代碼nodeslinks),然後首字母大寫和將get字附加到前面以嘗試使用getter方法。基本上你的EntryListContainer不遵循JSON(和代理的GSON)依賴的Java Bean Conventions。 所以它沒有打印任何東西,因爲你沒有getter方法用於getNodesgetLinks,你有getEntryList1getEntryList2

我敢肯定你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; 
    } 

}