2017-04-06 97 views
0

我試圖解析我的JSON響應,並將其複製/粘貼到Json2CSharp中,然後將2個新生成的類添加到當前類的底部。 (我不喜歡處理多個類)。那我遇到的問題是,當我試圖訪問生成的類RootObject我得到的類型的未處理的異常「Newtonsoft.Json.JsonSerializationException」發生在Newtonsoft.Json.dll
用Newtonsoft解析JSON拋出錯誤的格式錯誤

錯誤

附加信息:無法將當前JSON數組(例如[1,2,3])反序列化爲類型'Test.RootObject',因爲該類型需要JSON對象(例如{「name」:「value」})才能正確地反序列化。

即使語法轉換爲我,我沒有改變它。我需要改變什麼,以便這成爲有效的可用語法?

static void Main(string[] args) 
{ 
    string userid= "186exa"; 
    var url = "https/?rst=" + userid; 

    var connectionClient = new WebClient(); 
    connectionClient.Headers.Set("H", "XXXXXXXXXXXX"); 
    connectionClient.Headers.Set("uname", "user"); 
    connectionClient.Headers.Set("pswd", "pwd"); 
    var content = connectionClient.DownloadString(url); 
} 

編輯
這是類 - 將張貼JSON不久

public class List 
{ 
    public int id { get; set; } 
    public string cmu { get; set; } 
    public int lno { get; set; } 
    public string clr { get; set; } 
    public string name { get; set; } 
    public string origin { get; set; } 
    public string MajorStyle { get; set; } 
    public string Style { get; set; } 
    public string styleImage { get; set; } 
    public int hid { get; set; } 
    public string bqty { get; set; } 
    public int cask { get; set; } 
    public int local { get; set; } 
    public string city { get; set; } 
} 

public class RootObject 
{ 
    public string style { get; set; } 
    public List<List> List { get; set; } 
} 

這是retunred JSON

[{"Style":"Cajun","List":[{"id":1225,"cmu":"41.2","lno":10,"name":"Bear","origin":"Lake Sinclair, MO","MajorStyle":"Burn Yo Bottom","Style":"","styleImage":"","hid":1,"bqty":"1.00","cask":0,"local":0,"city":"Amsterdam"} 
+3

此代碼對問題沒有用處,請提供類和JSON。 – Simon

+0

請發佈您的JSON響應示例。 – Sxntk

+0

@Simon - 更新後的帖子,包括請求的信息。 –

回答

1

有過錯的兩件事情與你的代碼已發佈,

  1. 屬性「clr」在JSON中不存在。
  2. JSON過早結束,它應該在末尾有}}]是正確的。

    var o = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject[]>(s); 
    

    其中S是JSON字符串:

固定這兩個問題,代碼經過時它的類型RootObject [],按照正確分析在Newtonsoft。

+0

然後我將如何從變量o中提取信息? –

+0

該變量的類型爲RootObject [],在這種情況下,列表中只有一個項目可以使用o [0] .Style,o [0] .List [0] .id。在列表中有多個項目的情況下,您必須迭代。 – Simon

1

首先,你需要你的類比賽與你的JSON響應,讓你的類應該像

public class RootObject 
{ 
    public string Style { get; set; } 

    public List[] List { get; set; } 
} 

public class List 
{ 
    public int id { get; set; } 
    public string cmu { get; set; } 
    public int lno { get; set; } 
    public string clr { get; set; } 
    public string name { get; set; } 
    public string origin { get; set; } 
    public string MajorStyle { get; set; } 
    public string Style { get; set; } 
    public string styleImage { get; set; } 
    public int hid { get; set; } 
    public string bqty { get; set; } 
    public int cask { get; set; } 
    public int local { get; set; } 
    public string city { get; set; } 
} 

之後,你需要映射到對象

using Newtonsoft.Json; // Need this to work with JsonConvert 

string json = @"[ 
    { 
    'Style':'Cajun', 
    'List': 
     [ 
      { 
       'id':1225, 
       'cmu':'41.2', 
       'lno':10, 
       'name':'Bear', 
       'origin':'Lake Sinclair, MO', 
       'MajorStyle':'Burn Yo Bottom', 
       'Style':'', 
       'styleImage':'', 
       'hid':1, 
       'bqty':'1.00', 
       'cask':0, 
       'local':0, 
       'city':'Amsterdam' 
      } 
     ] 
    } 
]"; 
RootObject[] response = JsonConvert.DeserializeObject<RootObject[]>(json); 

響應使用關鍵字列表可能會導致錯誤,因爲還有一個名爲List的C#類,所以要小心。