2016-01-27 34 views
1

C#反序列化,我知道有許多關於反序列化JSON無數的職位,但我按照大部分職位的答案,而他們還沒有制定。我認爲我的主要問題是我似乎無法圍繞Json實際上是如何在結構上建立起來的。JSON與json.net

我有以下的JSON字符串從API:

{ 
    "totalCount_str": "3", 
    "items": [ 
    { 
     "standing": 10, 
     "corporation": { 
     "name": "borkedLabs", 
     "isNPC": false, 
     "href": "https://crest-tq.eveonline.com/corporations/98046548/", 
     "id_str": "98046548", 
     "logo": { 
      "32x32": { 
      "href": "http://imageserver.eveonline.com/Corporation/98046548_32.png" 
      }, 
      "64x64": { 
      "href": "http://imageserver.eveonline.com/Corporation/98046548_64.png" 
      }, 
      "128x128": { 
      "href": "http://imageserver.eveonline.com/Corporation/98046548_128.png" 
      }, 
      "256x256": { 
      "href": "http://imageserver.eveonline.com/Corporation/98046548_256.png" 
      } 
     }, 
     "id": 98046548 
     }, 
     "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/", 
     "contact": { 
     "id_str": "98046548", 
     "href": "https://crest-tq.eveonline.com/corporations/98046548/", 
     "name": "borkedLabs", 
     "id": 98046548 
     }, 
     "contactType": "Corporation" 
    }, 
    { 
     "standing": 10, 
     "character": { 
     "name": "xxxx yyyy", 
     "corporation": { 
      "name": "xxyshs", 
      "isNPC": false, 
      "href": "https://crest-tq.eveonline.com/corporations/98401169/", 
      "id_str": "98401169", 
      "logo": { 
      "32x32": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_32.png" 
      }, 
      "64x64": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_64.png" 
      }, 
      "128x128": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_128.png" 
      }, 
      "256x256": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_256.png" 
      } 
      }, 
      "id": 98401169 
     }, 
     "isNPC": false, 
     "href": "https://crest-tq.eveonline.com/characters/95161569/", 
     "capsuleer": { 
      "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/" 
     }, 
     "portrait": { 
      "32x32": { 
      "href": "http://imageserver.eveonline.com/Character/95161569_32.jpg" 
      }, 
      "64x64": { 
      "href": "http://imageserver.eveonline.com/Character/95161569_64.jpg" 
      }, 
      "128x128": { 
      "href": "http://imageserver.eveonline.com/Character/95161569_128.jpg" 
      }, 
      "256x256": { 
      "href": "http://imageserver.eveonline.com/Character/95161569_256.jpg" 
      } 
     }, 
     "id": 95161569, 
     "id_str": "95161569" 
     }, 
     "contact": { 
     "id_str": "95161569", 
     "href": "https://crest-tq.eveonline.com/characters/95161569/", 
     "name": "xxxx yyyy", 
     "id": 95161569 
     }, 
     "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/", 
     "contactType": "Character", 
     "watched": false, 
     "blocked": false 
    }, 
    { 
     "standing": -10, 
     "alliance": { 
     "id_str": "99000003", 
     "href": "http://crest.regner.dev/alliances/99000003/", 
     "id": 99000003, 
     "name": "One One Corporation Alliance" 
     }, 
     "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/", 
     "contact": { 
     "id_str": "99000003", 
     "href": "http://crest.regner.dev/alliances/99000003/", 
     "name": "One One Corporation Alliance", 
     "id": 99000003 
     }, 
     "contactType": "Alliance" 
    } 
    ], 
    "pageCount": 1, 
    "pageCount_str": "1", 
    "totalCount": 3 
} 

注意,該項目數組可以包含任意數量的「聯繫人」。

通過使用http://json2csharp.com/我把它轉換成C#類如下:

public class Contacts 
{ 
    public string totalCount_str { get; set; } 
    public Item[] items { get; set; } 
    public int pageCount { get; set; } 
    public string pageCount_str { get; set; } 
    public int totalCount { get; set; } 
} 

public class Item 
{ 
    public int standing { get; set; } 
    public Alliance alliance { get; set; } 
    public string href { get; set; } 
    public Contact contact { get; set; } 
    public string contactType { get; set; } 
    public Character character { get; set; } 
    public bool watched { get; set; } 
    public bool blocked { get; set; } 
} 

public class Alliance 
{ 
    public string id_str { get; set; } 
    public string href { get; set; } 
    public int id { get; set; } 
    public string name { get; set; } 
} 

public class Contact 
{ 
    public string id_str { get; set; } 
    public string href { get; set; } 
    public string name { get; set; } 
    public int id { get; set; } 
} 

public class Character 
{ 
    public string name { get; set; } 
    public Corporation corporation { get; set; } 
    public bool isNPC { get; set; } 
    public string href { get; set; } 
    public Capsuleer capsuleer { get; set; } 
    public Portrait portrait { get; set; } 
    public int id { get; set; } 
    public string id_str { get; set; } 
} 

public class Corporation 
{ 
    public string name { get; set; } 
    public bool isNPC { get; set; } 
    public string href { get; set; } 
    public string id_str { get; set; } 
    public Logo logo { get; set; } 
    public int id { get; set; } 
} 

public class Logo 
{ 
    public _32X32 _32x32 { get; set; } 
    public _64X64 _64x64 { get; set; } 
    public _128X128 _128x128 { get; set; } 
    public _256X256 _256x256 { get; set; } 
} 

public class _32X32 
{ 
    public string href { get; set; } 
} 

public class _64X64 
{ 
    public string href { get; set; } 
} 

public class _128X128 
{ 
    public string href { get; set; } 
} 

public class _256X256 
{ 
    public string href { get; set; } 
} 

public class Capsuleer 
{ 
    public string href { get; set; } 
} 

public class Portrait 
{ 
    public _32X32 _32x32 { get; set; } 
    public _64X64 _64x64 { get; set; } 
    public _128X128 _128x128 { get; set; } 
    public _256X256 _256x256 { get; set; } 
} 

然後試圖與反序列化它:

List<Contacts> tempList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Contacts>>(response.Content); 

我感謝所有幫助,可以讓我上正確的軌道。我得到一個錯誤,目前正試圖通過這種方式時:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ContactWatchlister.Models.Contacts]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 
Path 'totalCount_str', line 1, position 18. 

我現在堅持,因爲我不知道爲什麼它是無法理解「totalCount_str」成一個字符串。

我曾嘗試使用上的所有屬性[JsonProperty("attribute")]在我的類模型,可以正確的,但同樣的錯誤。

我很確定我正在做一些相當簡單,錯誤的事情,但是我無法把頭繞在它身上。希望你能幫助! :-)

回答

3

如果response.Content包含您描述我認爲的說法應該是JSON字符串:

var tempList = Newtonsoft.Json.JsonConvert.DeserializeObject<Contacts>(response.Content); 

您的JSON在最外面的 「級別」 的"totalCount_str"itemspageCountpageCount_strtotalCount領域,所以它只能被反序列化爲具有這些屬性(或屬性具有正確JsonAttribute s表示對應於那些字段名)一類。

你試圖將與下面的JSON,其中最外面的實體是一個數組的工作之一:

[ { 
    "totalCount_str": "3", 
    "items": [ 
     { 
     "standing": 10, 
     "corporation": { 
      "name": "borkedLabs", 
      "isNPC": false, 
      "href": "https://crest-tq.eveonline.com/corporations/98046548/", 
      "id_str": "98046548", 
      "logo": { 
      "32x32": { 
       "href": "http://imageserver.eveonline.com/Corporation/98046548_32.png" 
      }, 
      "64x64": { 
       "href": "http://imageserver.eveonline.com/Corporation/98046548_64.png" 
      }, 
      "128x128": { 
       "href": "http://imageserver.eveonline.com/Corporation/98046548_128.png" 
      }, 
      "256x256": { 
       "href": "http://imageserver.eveonline.com/Corporation/98046548_256.png" 
      } 
      }, 
      "id": 98046548 
     }, 
     "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/", 
     "contact": { 
      "id_str": "98046548", 
      "href": "https://crest-tq.eveonline.com/corporations/98046548/", 
      "name": "borkedLabs", 
      "id": 98046548 
     }, 
     "contactType": "Corporation" 
     }, 
     { 
     "standing": 10, 
     "character": { 
      "name": "xxxx yyyy", 
      "corporation": { 
      "name": "xxyshs", 
      "isNPC": false, 
      "href": "https://crest-tq.eveonline.com/corporations/98401169/", 
      "id_str": "98401169", 
      "logo": { 
       "32x32": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_32.png" 
       }, 
       "64x64": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_64.png" 
       }, 
       "128x128": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_128.png" 
       }, 
       "256x256": { 
       "href": "http://imageserver.eveonline.com/Corporation/98401169_256.png" 
       } 
      }, 
      "id": 98401169 
      }, 
      "isNPC": false, 
      "href": "https://crest-tq.eveonline.com/characters/95161569/", 
      "capsuleer": { 
      "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/" 
      }, 
      "portrait": { 
      "32x32": { 
       "href": "http://imageserver.eveonline.com/Character/95161569_32.jpg" 
      }, 
      "64x64": { 
       "href": "http://imageserver.eveonline.com/Character/95161569_64.jpg" 
      }, 
      "128x128": { 
       "href": "http://imageserver.eveonline.com/Character/95161569_128.jpg" 
      }, 
      "256x256": { 
       "href": "http://imageserver.eveonline.com/Character/95161569_256.jpg" 
      } 
      }, 
      "id": 95161569, 
      "id_str": "95161569" 
     }, 
     "contact": { 
      "id_str": "95161569", 
      "href": "https://crest-tq.eveonline.com/characters/95161569/", 
      "name": "xxxx yyyy", 
      "id": 95161569 
     }, 
     "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/", 
     "contactType": "Character", 
     "watched": false, 
     "blocked": false 
     }, 
     { 
     "standing": -10, 
     "alliance": { 
      "id_str": "99000003", 
      "href": "http://crest.regner.dev/alliances/99000003/", 
      "id": 99000003, 
      "name": "One One Corporation Alliance" 
     }, 
     "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/", 
     "contact": { 
      "id_str": "99000003", 
      "href": "http://crest.regner.dev/alliances/99000003/", 
      "name": "One One Corporation Alliance", 
      "id": 99000003 
     }, 
     "contactType": "Alliance" 
     } 
    ], 
    "pageCount": 1, 
    "pageCount_str": "1", 
    "totalCount": 3 } ] 
+0

衛生署...你是絕對正確的。之後,我改變成,和我的固定整數雙打。 (10.0不能是一個int)它完美的工作! 謝謝! – Frederik