2015-06-21 108 views
0

我試圖將JSON映射到Java使用gson.I是成功的寫入邏輯,但在得到輸出unsuccesful。張貼的是我的JSON和Java文件。任何幫助將是高度讚賞。JSON映射到Java返回空值

這是我得到

value:null 

貼在下面的輸出爲以.json文件

{ 
    "catitem": { 
     "id": "1.196289", 
     "src": "http://feeds.reuters.com/~r/reuters/MostRead/~3/PV-SzW7Pve0/story06.htm", 
     "orig_item_date": "Tuesday 16 June 2015 07:01:02 PM UTC", 
     "cat_id": "1", 
     "heding": "Putin says Russia beefing up nuclear arsenal", 
     "summary": "KUvdfbefb bngfb", 
     "body": { 
      "bpart": [ 
       "KUBINKA,dvdvdvdvgbtgfdnhfbnrtdfbcv dbnfg" 
      ] 
     } 
    } 
} 

貼在下面的代碼是我的.java文件

public class offc { 

    public static void main(String[] args) { 
     JsonReader jr = null; 

     try { 
      jr = new JsonReader(new InputStreamReader(new FileInputStream(
        "C:\\Users\\rishii\\IdeaProjects\\rishi\\src\\file3.json"))); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     Doll s = new Doll(); 

     Gson g = new Gson(); 
     Doll sr1 = g.fromJson(jr, Doll.class); 
     System.out.println(sr1); 
    } 
} 

以下發布的是Doll.java的代碼

class Doll { 

    private catitem ct; 

    public void setCt(catitem ct) { 

     this.ct = ct; 
    } 

    public catitem getCt() { 
     return ct; 
    } 

    @Override 
    public String toString() 

    { 
     return "value:" + ct; 
    } 

    class catitem { 
     private String id; 
     private String src; 
     private String orig_item_date; 
     private String cat_id; 
     private String heding; 
     private String summary; 
     private body ber; 

     catitem(String id, String src, String orig_item_date, String cat_id, String heding, 
       String summary) { 
      this.id = id; 
      this.src = src; 
      this.orig_item_date = orig_item_date; 
      this.cat_id = cat_id; 
      this.heding = heding; 
      this.summary = summary; 
     } 

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

     public String getId() { 
      return id; 
     } 

     public void setSrc(String src) { 
      this.src = src; 
     } 

     public String getSrc() { 
      return src; 
     } 

     public void setOrig_item_date(String Orig_item_date) { 
      this.orig_item_date = Orig_item_date; 
     } 

     public String getOrig_item_date() { 

      return getOrig_item_date(); 
     } 

     public void setCat_id(String cat_id) { 

      this.cat_id = cat_id; 
     } 

     public String getCat_id() { 
      return cat_id; 
     } 

     public void setHeding(String heding) { 
      this.heding = heding; 
     } 

     public String getHeding() { 
      return heding; 
     } 

     public void setSummary(String summary) { 
      this.summary = summary; 
     } 

     public String getSummary() { 
      return summary; 
     } 

     public void setBer(body ber) { 
      this.ber = ber; 
     } 

     public body getBer() { 
      return ber; 
     } 

     @Override 
     public String toString() { 
      return "id:" + id + "cat_id" + cat_id + "summary" + summary + "orig_date" 
        + orig_item_date + "heding" + heding; 
     } 
    } 

    class body { 
     private String bpart; 

     public void setBpart(String r) { 

      this.bpart = r; 
     } 

     public String getBpart() { 

      return bpart; 
     } 

     @Override 
     public String toString() { 

      return "hiii"; 
     } 
    } 

} 

回答

1

該問題是在類娃娃,你有一個字段ct,但在json catitem。將字段ct重命名爲catitem,或者如果您正在使用Gson,請使用@SerializedName("catitem")提交ct,它將起作用。

+0

非常感謝你!它的工作:) :) –

+0

而且'私人身體;' - >'私人身體;'' – saka1029