2017-02-11 86 views
1

有得到一個JSON字符串轉換成C#的DataTable的問題...問題JSON字符串轉換爲DataTable的C#

這裏是被從API返回我的代碼JSON字符串的例子

{ 
    "Code": 0, 
    "Result": [ 
    { 
     "ID": 1, 
     "Reference": "101", 
     "Asset": 200, 
     "Event": 1, 
     "DateEventStart": "2/10/2017 9:08:33 PM", 
     "DateEvent": "2/11/2017 1:00:14 AM" 
    } 
] 
} 

所以從API進來,我讀它,然後嘗試創建這樣

 var response = (HttpWebResponse)httpWebRequest.GetResponse(); 
     var encoding = Encoding.GetEncoding(response.CharacterSet); 
     var responseStream = response.GetResponseStream(); 
     var reader = new StreamReader(responseStream, encoding); 
     responsetext = reader.ReadToEnd(); 

     var table = JsonConvert.DeserializeObject<DataTable>(responsetext); 

一個DataTable然而,這提供了以下錯誤:

Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1

我新來處理JSON在C#中,所以任何幫助表示讚賞。我認爲我的字符串在某些方面無法解析,但我不知道。如果有更多的信息需要讓我知道

編輯: 好吧,我跟着星星喬放的鏈接 - 是有道理的。我現在得到一個不同的錯誤,我想是因爲我已經把進級了JSON被deserialised成 -

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WindowsFormsApplication7.TrackingResponse]' 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) 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 'Code', line 1, position 8.

這裏是一個類......是不是因爲我不包括'守則」和‘從JSON結果’(見上面的字符串)

public class TrackingResponse 
{ 
    public string ID { get; set; } 
    public string Reference { get; set; } 
    public string Asset { get; set; } 
    public string Event { get; set; } 
    public string DateEventStart { get; set; } 
    public string DateEvent { get; set; } 

} 
+0

檢查此[convert-json-to-datatable](http://stackoverflow.com/questions/11981282/convert-json-to-datatable) –

+0

看起來像[從Json字符串反序列化嵌套的DataSet與Json.NET](https://stackoverflow.com/a/42090584/3744182)。正如在那個答案中,你需要一個包裝器根對象來包含你的'DataTable'。這是否足以回答您的問題,還是您需要特定的根對象? – dbc

+0

謝謝@XingxingQiao,看看我上面的更多評論 – reviloSlater

回答

3

你的問題是,你的根JSON容器不是一個數組,它是一個對象。正如在JSON standard解釋:

  • 數組是值的有序集合。一個數組以[(左括號)開頭並以](右括號)結尾。值由,(逗號)分隔。

  • 一個對象是一組無名稱/值對。一個對象以{(左大括號)開頭,以}(右大括號)結尾。

Newtonsoft嘗試將DataTableIEnumerable, List, and Array類型以及對象)從所述陣列映射,並且爲根容器不是一個數組,它失敗預期StartArray的,得到了在StartObject錯誤。

由於您的根容器是具有兩個屬性"Code""Result"的對象,因此您需要創建一個POCO來綁定這些屬性。現在,"Result"的值是的數組,因此可以映射到DataTable,或者對於某個適當的T可以映射到List<T>。爲了產生必要的類,你可以使用一個代碼生成工具,如http://json2csharp.com/或Visual Studio Paste JSON As Classes,並得到類似如下(稍作修改,使根對象通用):

public class Result 
{ 
    public int ID { get; set; } 
    public string Reference { get; set; } 
    public int Asset { get; set; } 
    public int Event { get; set; } 
    public string DateEventStart { get; set; } 
    public string DateEvent { get; set; } 
} 

public class RootObject<T> 
{ 
    public int Code { get; set; } 
    public T Result { get; set; } 
} 

現在,您可以反序列化您的JSON作爲RootObject<DataTable>RootObject<List<Result>>

var rootOfList = JsonConvert.DeserializeObject<RootObject<List<Result>>>(responsetext); 

或者

var rootOfTable = JsonConvert.DeserializeObject<RootObject<DataTable>>(responsetext); 
DataTable table = rootOfTable.Result; 

樣品fiddle

+0

這幫助我非常 - 非常感謝(並感謝解釋它,而不是隻是在這裏粘貼一些代碼) – reviloSlater