2013-07-03 50 views
3

我旁邊JSON回調:JSON.NET Deserealization

{ 
    "id":"1", 
    "jsonrpc":"2.0", 
    "result":{ 
    "articles":[ 
    { 
     "date":1367582340000, 
     "id":6917, 
     "title":"Some Title", 
     "author":"Name Surname", 
     "event_date":1367584560000, 
     "text":"blabla"    
    } 
    ] 
} 
} 

我想用JSON.NET

反序列化它,我已經寫了下面的代碼:

class News 
    { 
     private string jsonrpc; 
     private string id; 
     private Result result; 

     [JsonProperty("jsonrpc")] 
     public string Jsonrpc 
     { 
      get { return jsonrpc; } 
      set { jsonrpc = value; } 
     } 

     [JsonProperty("id")] 
     public string Id 
     { 
      get { return id; } 
      set { id = value; } 
     } 

     [JsonProperty("result")] 
     public Result Result 
     { 
      get { return result; } 
      set { result = value; } 
     } 
    } 
    public class Result 
    { 
     public List<Article> articles; 
     [JsonProperty("articles")] 
     public List<Article> Articles 
     { 
      get { return articles; } 
      set { articles = value; } 
     } 
    } 
    public class Article 
    { 
     public string text; 
     public int id; 
     public int date; 
     public string title; 
     public string author; 
     public string imageURL; 
     [JsonProperty("text")] 
     public string Text 
     { 
      get { return text; } 
      set { text = value; } 
     } 
     [JsonProperty("id")] 
     public int Id 
     { 
      get { return id; } 
      set { id = value; } 
     } 
     [JsonProperty("date")] 
     public int Date 
     { 
      get { return date; } 
      set { date = value; } 
     } 
     [JsonProperty("title")] 
     public string Title 
     { 
      get { return title; } 
      set { title = value; } 
     } 
     [JsonProperty("author")] 
     public string Author 
     { 
      get { return author; } 
      set { author = value; } 
     } 
     [JsonProperty("imageURL")] 
     public string ImageURL 
     { 
      get { return imageURL; } 
      set { imageURL = value; } 
     } 
    } 

用法:

string JSON = reader.ReadToEnd(); 
News ent = JsonConvert.DeserializeObject<News>(JSON) as News; 

出現錯誤:

型「Newtonsoft.Json.JsonSerializationException」的異常出現在Newtonsoft.Json.DLL但在用戶代碼

有什麼問題沒有處理?

回答

2

A member with the name 'articles' already exists on 'Result'. Use the JsonPropertyAttribute to specify another name.

充分利用articlesprivate

private List<Article> articles; 

A member with the name 'text' already exists on 'Article'. Use the JsonPropertyAttribute to specify another name.

再次相同:

private string text; 
private int id; 
private int date; 
private string title; 
private string author; 
private string imageURL; 

或更好 - 使用自動實現的屬性和扔掉的領域,即

[JsonProperty("text")] 
public string Text {get;set;} 

Arithmetic operation resulted in an overflow.

Date一個long

[JsonProperty("date")] 
public long Date {get;set;} 

這裏是我的整個工人階級算賬:

class News 
{ 
    [JsonProperty("jsonrpc")] 
    public string Jsonrpc {get;set;} 
    [JsonProperty("id")] 
    public string Id { get; set; } 
    [JsonProperty("result")] 
    public Result Result { get; set; } 
} 
public class Result 
{ 
    private List<Article> articles = new List<Article>(); 
    [JsonProperty("articles")] 
    public List<Article> Articles { get { return articles; }} 
} 
public class Article 
{ 
    [JsonProperty("text")] 
    public string Text {get;set;} 
    [JsonProperty("id")] 
    public int Id {get;set;} 
    [JsonProperty("date")] 
    public long Date {get;set;} 
    [JsonProperty("title")] 
    public string Title {get;set;} 
    [JsonProperty("author")] 
    public string Author { get; set; } 
    [JsonProperty("imageURL")] 
    public string ImageURL { get; set; } 
} 

需要注意的是,如果你只反序列化,你甚至都不需要JsonProperty - 因爲除了這種情況之外,這些名字都是相同的,如果你也是序列化

+0

Hooray。十分感謝。你解決了我的問題。 – Cheese

+2

@奶酪是公平的,主要是我做的只是*讀取異常信息*;這就是3個引號(「名稱的成員...」和「算術運算...」) - 串行器*告訴你*問題是什麼 –