错误将json转换为vb.net中的datatable

人气:200 发布:2022-09-22 标签: asp.net json vb.net datatable vb ASP.NET VB.NET

问题描述

我通过http发送以下字符串作为json:

  Dim  json 作为 字符串 =   [{ +     name:jocelyne +    , +     mo: jocelyne +     } +  ,{ +     name:eliane +    , +      mo:12345678 +    } +   ]  

i需要将此json数组转换为vb.net中的数据表 i尝试使用NewtonSoft.json.dll:

  Dim  jss  As  新 System.Web .Script.Serialization.JavaScriptSerializer()  Dim  deserializedProduct  As  Dictionary( Of   String  ,字符串)= JsonConvert.DeserializeObject( 字典( Of  字符串,字符串))(json)

但我一直在那里没有可用于当前位置的源代码,虽然我添加了对我的项目的引用并且它在bin文件中.. 有没有办法解决这个问题,或者有没有人有另一种方法将json数组转换为vb.net中的数据表?

解决方案

< blockquote>我修复了它: 我试图将JSON字符串反序列化为应该表示对象的Dictionary,但是我的JSON字符串包含两个对象的数组而不是单个物体。 所以应该使用List(Of Dictionary(Of String,String))而不是Dictionary(Of String,String):

 < span class =code-keyword> Sub  Main   Dim  json  As  字符串 =   [{ + < span class =code-string>    name:jocelyne +    , +     mo:jocelyne +    } +   ,{ +     name:eliane +    , +     mo:12345678 +    } +  ]   Dim  deserializedProduct  As  List ( 字典(  字符串,< span class =code-keyword> String ))= JsonConvert.D eserializeObject(的列表(的字典( of  字符串,字符串)))(json) deserializedProduct.Dump() 结束  Sub  

如果我需要DataTable,我只需使用

  Sub  Main   Dim  json  As  字符串 =   [{ +     name:jocelyne +    ,  +     mo:jocelyne +     } +  ,{ +   < span class =code-string>  name:eliane +    , + < span class =code-string>    mo:12345678 +    } +  ]   Dim 表 As  DataTable = JsonConvert.DeserializeObject( Of  DataTable)(json) table.Dump() 结束  Sub  

i am receiving through http post the following string as json:

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 :

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?

解决方案

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):

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

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

935