2014-03-29 200 views
0

我不能爲我的生活弄清楚如何使用傑克遜解析這個json文件到對象。傑克遜json到對象

這裏是我的JSON文件:

{ 
    "file": "phrases", 
    "use": "quotes", 
    "famous_phrases": [ 
     { 
     "phrase1": "one for all", 
     "phrase2": "all for one", 
     "id": 1 
     }, 
     { 
     "phrase1": "four scores", 
     "phrase2": "and seven years ago", 
     "id": 17 
     }, 
     { 
     "phrase1": "elementary", 
     "phrase2": "my dear watson", 
     "id": 22 
     } 
    ] 
    } 

我嘗試這樣做:

BufferedReader fileReader = new BufferedReader(new FileReader("./test.json")); 
ObjectMapper mapper = new ObjectMapper(); 
JsonNode quotes = mapper.readValue(fileReader, JsonNode.class); 
quotes = quotes.get("famous_phrases"); 
TypeReference<List<Quotes>> phrases = new TypeReference<List<Quotes>>(){}; 
List<Quotes> q = mapper.readValue(quotes.traverse(), phrases); 
for (Phrases element : q) { 
    System.out.println(element.getPhrase1()); 
} 

爲一個POJO,我做了,但我想我可能會做出錯誤的POJO。我定義了所有的屬性(文件,使用,名詞),每個都有自己的設置和獲取方法。任何幫助到這將不勝感激!

+0

你在那裏有沒有有效的JSON。它包含2個由逗號分隔的JSON對象。所以你不能解析它。 –

+0

此JSON無效 – Bozho

+0

您是如何獲得json的? – Bozho

回答

1

JSON文件:

{ 
    "file": "phrases", 
    "use": "quotes", 
    "famous_phrases": [ 
    { 
     "phrase1": "one for all", 
     "phrase2": "all for one", 
     "id": 1 
    }, 
    { 
     "phrase1": "four scores", 
     "phrase2": "and seven years ago", 
     "id": 17 
    }, 
    { 
     "phrase1": "elementary", 
     "phrase2": "my dear watson", 
     "id": 22 
    } 
    ] 
} 

豆類:

public class Quotes { 

    private String file; 
    private String use; 
    private List<Phrases> famous_phrases; 

    public String getFile() { 
     return file; 
    } 

    public void setFile(String file) { 
     this.file = file; 
    } 

    public String getUse() { 
     return use; 
    } 

    public void setUse(String use) { 
     this.use = use; 
    } 

    public List<Phrases> getFamous_phrases() { 
     return famous_phrases; 
    } 

    public void setFamous_phrases(List<Phrases> famous_phrases) { 
     this.famous_phrases = famous_phrases; 
    } 

} 

public class Phrases { 

    private String phrase1; 
    private String phrase2; 
    private String id; 

    public String getPhrase1() { 
     return phrase1; 
    } 

    public void setPhrase1(String phrase1) { 
     this.phrase1 = phrase1; 
    } 

    public String getPhrase2() { 
     return phrase2; 
    } 

    public void setPhrase2(String phrase2) { 
     this.phrase2 = phrase2; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

} 

閱讀JSON file:

BufferedReader fileReader = new BufferedReader(new FileReader("./test.json")); 
ObjectMapper mapper = new ObjectMapper(); 
Quotes quotes = mapper.readValue(fileReader, Quotes.class); 
for (Phrases element : quotes.getFamous_phrases()) { 
    System.out.println(element.getPhrase1()); 
} 
+0

謝謝,修復了我的json後,這個工作好得多。 – imakeitrayne

0

你必須定義一個根節點(纏上{}您的JSON代碼)

{ 
    "member1": { 
     "file": "phrases", 
     "use": "quotes" 
    }, 
    "member2": { 
     "famous_phrases": [{ 
      "phrase1": "one for all", 
      "phrase2": "all for one", 
      "id": 1 
     }, 
     { 
      "phrase1": "four scores", 
      "phrase2": "and seven years ago", 
      "id": 17 
     }, 
     { 
      "phrase1": "elementary", 
      "phrase2": "my dear watson", 
      "id": 22 
     } 
    }] 
} 
+0

感謝您的幫助,看到我的json文件不正確後,這也有很大幫助。 – imakeitrayne