2016-10-02 96 views
0

如何將我的JSON字符串轉換爲類C# - 如何JSON字符串轉換爲

這是我的JSON

{ 「的$ id」: 「1」, 「結果」:{ 「$ ID」: 「2」, 「日期時間」:23821964, 「列表」:[{ 「$ ID」: 「3」, 「用戶ID」:302, 「UID」: 「302_UID」, 「Title」:「شیدکو」, 「Sender」:「شیدکو」, 「Answer」:「」, 「註釋」: 「測試2」, 「ProductTitle」:空, 「CommentId」:77, 「標識」: 「http://www.domain.com/Commercial/User/302/Logo/tmpF0BF.jpg」, 「日期」:24302057, 「AnswerDate」:-2123661683, 「 AnswerEdit「:假, 」CommentEdit「:假, 」ForfeitCount「:0, 」RewardCount「:0, 」ThisCountReport「:2, 」報道「:[{ 」$ ID「: 」4「, 「BlockerId」:355, 「Title」:「محتوایغیراخلاقی」, 「Date」:-19527396, 「ForfeitCount」:0, 「RewardCount」:0 },{ 「的$ id」: 「5」, 「BlockerId」:355, 「標題」: 「محتوایغیرمرتبط」, 「日期」:-19527382, 「ForfeitCount」 :0, 「RewardCount」:0 }], 「寶石」:0 },{ 「$ ID」: 「6」, 「用戶ID」:302, 「UID」: 「302_UID」, 「Title」:「شیدکو」, 「Sender」:「شیدکو」, 「Answer」:「」, 「Comment」:「test 2」, 「ProductTitle」:null, 「CommentId」:77, 「標誌」: 「http://www.www.domain.com/Commercial/User/302/Logo/tmpF0BF.jpg」, 「日期」:24302057, 「AnswerDate」:-2123661683, 「AnswerEdit」:假的, 「CommentEdit」:假

 }] 

    }, 
    "StatusCode": "Created", 
    "Description": null 
} 

我做這些步驟,但沒有任何反應

JObject json1 = JObject.Parse(strMyJson); 
      _CommentAdmindto flight = Newtonsoft.Json.JsonConvert.DeserializeObject<_CommentAdmindto>(json1.ToString()); 
      _CommentAdmindto deserializedProduct = JsonConvert.DeserializeObject<_CommentAdmindto>(json); 
      _CommentAdmindto deserializedProduct1 = ConvertJsonToClass<_CommentAdmindto>(strMyJson); 
      JsonSerializer serializer = new JsonSerializer(); 
      _CommentAdmindto p = (_CommentAdmindto)serializer.Deserialize(new JTokenReader(strMyJson), typeof(_CommentAdmindto)); 

這裏是我的類和函數:

public static T ConvertJsonToClass<T>(string json) 
     { 
      System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      return serializer.Deserialize<T>(json); 
     } 

    } 



    public class _CommentAdmindto 
    { 
     public long dateTime { get; set; } 

     public IQueryable<CommentDtoAdmin> list { get; set; } 


    } 


    public class CommentDtoAdmin 
    { 
     public long UserId { get; set; } 
     public string UID { get; set; } 
     public string Title { get; set; } 
     public string Sender { get; set; } 
     public string Answer { get; set; } 
     public string Comment { get; set; } 

     public string ProductTitle { get; set; } 
     public long CommentId { get; set; } 

     public string Logo { get; set; } 
     public long Date { get; set; } 

     public long AnswerDate { get; set; } 

     public bool AnswerEdit { get; set; } 

     public bool CommentEdit { get; set; } 


    } 
+0

我想它是一個重複的問題http://stackoverflow.com/questions/4718888/how-to-convert-json-to-c-sharp-classes –

+0

@HussainPatel在你的鏈接,他們沒有把它添加到類,請看看它吧 – Dav

+0

你的JSON與你的模型不匹配 –

回答

4

你的模式應該是與此類似(對於無效的C#的名字,你可以使用JsonProperty屬性):

public class Reported 
{ 
    [JsonProperty("$id")] 
    public string id { get; set; } 
    public int BlockerId { get; set; } 
    public string Title { get; set; } 
    public int Date { get; set; } 
    public int ForfeitCount { get; set; } 
    public int RewardCount { get; set; } 
} 

public class List 
{ 
    [JsonProperty("$id")] 
    public string id { get; set; } 
    public int UserId { get; set; } 
    public string UID { get; set; } 
    public string Title { get; set; } 
    public string Sender { get; set; } 
    public string Answer { get; set; } 
    public string Comment { get; set; } 
    public object ProductTitle { get; set; } 
    public int CommentId { get; set; } 
    public string Logo { get; set; } 
    public int Date { get; set; } 
    public int AnswerDate { get; set; } 
    public bool AnswerEdit { get; set; } 
    public bool CommentEdit { get; set; } 
    public int ForfeitCount { get; set; } 
    public int RewardCount { get; set; } 
    public int ThisCountReport { get; set; } 
    public List<Reported> Reported { get; set; } 
    public int Gem { get; set; } 
} 

public class Result 
{ 
    [JsonProperty("$id")] 
    public string id { get; set; } 
    public int dateTime { get; set; } 
    public List<List> list { get; set; } 
} 

public class RootObject 
{ 
    [JsonProperty("$id")] 
    public string id { get; set; } 
    public Result Result { get; set; } 
    public string StatusCode { get; set; } 
    public object Description { get; set; } 
} 

現在你可以反序列化作爲

var result = JsonConvert.DeserializeObject<RootObject>(jsonstring); 

BTW:http://json2csharp.com/可以幫助猜測使用json時你的模型。

+0

如果您使用的是Visual Studio 2015,它具有內置的JSON類功能。只需打開一個新的空白類文件,然後選擇編輯>選擇性粘貼> JSON作爲類。也適用於XML。並不總是完美但很好。在這種情況下唯一錯過的是ID變量的屬性修飾。 – PMV

+0

非常感謝:) – Dav

0

我認爲你的json字符串沒有正確的語法。它看起來像']'(數組的末尾),最後的'}'缺失。

這是消除所有的「\」後,給JSON文件從你的字符串時,什麼在Visual Studio編輯告訴我

+0

這不是答案 - 你應該把這個在評論中,如果出現問題 –

+0

問題我編輯它,JSON現在工作正常,但我無法得到我問 – Dav

1

你似乎在試圖用很多不同的方式來反序列化,但你不有一個完整的結構來實際匹配json。您錯過了外部類(代表完整對象),至少Newtonsoft.Json無法反序列化爲IQueryable,因此我將其更改爲IEnumerable。

string strMyJson = "{\"$id\":\"1\",\"Result\":{\"$id\":\"2\",\"dateTime\":23826985,\"list\":[{\"$id\":\"3\",\"UserId\":302,\"UID\":\"302_UID\",\"Title\":\"شیدکو\",\"Sender\":\"شیدکو\",\"Answer\":\"\",\"Comment\":\"test 2\",\"ProductTitle\":null,\"CommentId\":77,\"Logo\":\"http://www.domain.com/Commercial/User/302/Logo/tmpF0BF.jpg\",\"Date\":24307078,\"AnswerDate\":-2123656662,\"AnswerEdit\":false,\"CommentEdit\":false,\"ForfeitCount\":0,\"RewardCount\":0,\"ThisCountReport\":2,\"Reported\":[{\"$id\":\"4\",\"BlockerId\":355,\"Title\":\"محتوای غیر اخلاقی\",\"Date\":-19527396,\"ForfeitCount\":0,\"RewardCount\":0},{\"$id\":\"5\",\"BlockerId\":355,\"Title\":\"محتوای غیر مرتبط\",\"Date\":-19527382,\"ForfeitCount\":0,\"RewardCount\":0}],\"Gem\":0},{\"$id\":\"6\",\"UserId\":302,\"UID\":\"302_UID\",\"Title\":\"شیدکو\",\"Sender\":\"شیدکو\",\"Answer\":\"\",\"Comment\":\"test 2\",\"ProductTitle\":null,\"CommentId\":77,\"Logo\":\"http://www.domain.com/Commercial/User/302/Logo/tmpF0BF.jpg\",\"Date\":24307078,\"AnswerDate\":-2123656662,\"AnswerEdit\":false,\"CommentEdit\":false}],\"StatusCode\":\"Created\",\"Description\":null}}"; 


var result = JsonConvert.DeserializeObject<Wrapper>(strMyJson); 

與看起來像這樣的類:

public class Wrapper 
{ 
    public _CommentAdmindto Result { get; set; } 
} 
public class _CommentAdmindto 
{ 
    public long dateTime { get; set; } 

    public IEnumerable<CommentDtoAdmin> list { get; set; } 
} 

CommentDtoAdmin正在尋找相同。 雖然我必須說,這隻能幫助你進行反序列化。

+0

謝謝,它真的更好,但我現在有另一個錯誤,工作新的錯誤。在System.Web.Extensions.dll中發生類型'System.ArgumentException'的異常,但未在用戶代碼中處理 其他information:類型爲'System.Collections.Generic.List'1 [AdminApp._CommentReported]'ca的對象不能轉換爲'System.Linq.IQueryable'1 [AdminApp._CommentReported]'類型。 – Dav

1

首先,$id"屬性是Json.NET添加的綜合屬性,用於跟蹤和保存對同一對象的多個引用。有關詳細信息,請參閱PreserveReferencesHandling setting

因此,如果您暫時刪除"$id"屬性,你可以上傳你的JSON來http://json2csharp.com/並得到以下數據模型:

public class Reported 
{ 
    public int BlockerId { get; set; } 
    public string Title { get; set; } 
    public int Date { get; set; } 
    public int ForfeitCount { get; set; } 
    public int RewardCount { get; set; } 
} 

public class CommentDtoAdmin 
{ 
    public int UserId { get; set; } 
    public string UID { get; set; } 
    public string Title { get; set; } 
    public string Sender { get; set; } 
    public string Answer { get; set; } 
    public string Comment { get; set; } 
    public object ProductTitle { get; set; } 
    public int CommentId { get; set; } 
    public string Logo { get; set; } 
    public int Date { get; set; } 
    public int AnswerDate { get; set; } 
    public bool AnswerEdit { get; set; } 
    public bool CommentEdit { get; set; } 
    public int ForfeitCount { get; set; } 
    public int RewardCount { get; set; } 
    public int ThisCountReport { get; set; } 
    public List<Reported> Reported { get; set; } 
    public int Gem { get; set; } 
} 

public class Result 
{ 
    public int dateTime { get; set; } 
    public List<CommentDtoAdmin> list { get; set; } 
} 

public class RootObject 
{ 
    public Result Result { get; set; } 
    public string StatusCode { get; set; } 
    public string Description { get; set; } 
} 

我再修改返回的模型如下:

  • 對於list類型,我使用名稱CommentDtoAdmin
  • 我將Description屬性的類型設置爲string

現在你的JSON可以反序列化和重新序列如下:

var settings = new JsonSerializerSettings 
{ 
    PreserveReferencesHandling = PreserveReferencesHandling.Objects, 
}; 
var root = JsonConvert.DeserializeObject<RootObject>(json1, settings); 

var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings); 

注意Json.NET有沒有內置的邏輯接口IQueryable<T>反序列化到具體類型,所以我有離開物業public List<CommentDtoAdmin> list { get; set; }。您可以隨時生成使用AsQueryable()名單可查詢:

var queryable = root.Result.list.AsQueryable(); 

樣品fiddle