2013-10-22 18 views
-1

我收到通过HTTP POST下面的字符串作为JSON:错误转换JSON到数据表在vb.net

Dim json As String = "[{" + """name"":""jocelyne" + """," + """mo"":""jocelyne" + """}" + ",{" + """name"":""eliane" + """," + """mo"":""12345678" + """}" + "]" 

我需要这个JSON数组转换成一个DataTable在vb.net

我使用NewtonSoft.json.dll尝试:

Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer() 
Dim deserializedProduct As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(json) 

,但我不断收到有可用于当前位置没有源代码,虽然我加入了参考我的项目,它在bin文件..

实际的错误是:

Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'. 

是有办法解决这个问题还是没有人有另一种方式来JSON数组转换为vb.net一个DataTable?

+0

其不正确的json – Shruti

+0

@Shruti在json中有什么不正确? – User7291

+0

你可以验证你的JSON使用这个链接http://jsonlint.org/ ..在这里你的JSON和你会知道 – Shruti

回答

1

您尝试将JSON字符串反序列化为Dictionary,它应该代表一个对象,但是您的JSON字符串包含两个对象而不是单个对象的数组。

所以,你应该用List(Of Dictionary(Of String, String))而不是Dictionary(Of String, String)

LINQPad:

Sub Main 
    Dim json As String = "[{" + """name"":""jocelyne" + """," + """mo"":""jocelyne" + """}" + ",{" + """name"":""eliane" + """," + """mo"":""12345678" + """}" + "]" 
    Dim deserializedProduct As List(Of Dictionary(Of String, String)) = JsonConvert.DeserializeObject(Of List(Of Dictionary(Of String, String)))(json) 
    deserializedProduct.Dump() 
End Sub 

enter image description here

如果你想要一个DataTable,只需使用

Sub Main 
    Dim json As String = "[{" + """name"":""jocelyne" + """," + """mo"":""jocelyne" + """}" + ",{" + """name"":""eliane" + """," + """mo"":""12345678" + """}" + "]" 
    Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(json) 
    table.Dump() 
End Sub 

enter image description here

+0

你可以请检查我以下问题:http://stackoverflow.com/questions/19535504/json-object-into-a-datatable-and-a-string – User7291

+0

我需要得到两个数据表 – User7291