Click here to Skip to main content
15,077,265 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am receiving through http post the following string as json:
VB
Dim json As String = "[{" + """name"":""jocelyne" + """," + """mo"":""jocelyne" + """}" + ",{" + """name"":""eliane" + """," + """mo"":""12345678" + """}" + "]"


i need to convert this json array to a datatable in vb.net

i tried using NewtonSoft.json.dll :
VB
Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim deserializedProduct As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(json)


but i keep getting there is no source code available for the current location although i added the reference to my project and it's in the bin file ..

is there a way to fix this or does anyone have another way to convert a json array to a datatable in vb.net?
Posted
Updated 22-Oct-13 2:28am
v2
Comments
ZurdoDev 22-Oct-13 8:31am
   
What's the actual error?
Jocelyne El Khoury 22-Oct-13 8:47am
   
Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'.
Sinisa Hajnal 23-Mar-16 7:05am
   
Well, there is your error, you have to change Dictionary (string, string) to something else. You need string, object since you have array of objects containing properties name and mo. Create the object of type Person and Deserialize into List(Of Person)

1 solution

i fixed it :

i'm trying to deserialize the JSON string into a Dictionary which should represent an object, but my JSON string contains an array of two objects instead of a single object.

So ishould use List(Of Dictionary(Of String, String)) instead of Dictionary(Of String, String):

VB
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



If i want a DataTable, i just use

VB
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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900