错误将json转换为vb.net中的datatable [英] Error convert json to datatable in vb.net

查看:339
本文介绍了错误将json转换为vb.net中的datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过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


这篇关于错误将json转换为vb.net中的datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆