将Json对象转换为数据和字符串 [英] Json Object into a datatable and a string
问题描述
{
标题: {
MCC:415,
F0:0,
REG ID:0
},
:[
{
name:jocelyne,
mo:jocelyne
},
{
name eliane,
mo:12345678
}
]
}
我需要把数据下的联系人放在datatable中,并将header下的数据反序列化为3个变量...
我试过了:
Dim deserializedProduct As List(Of String(Of String,String))= JsonConvert.DeserializeObject(Of List(Of Dictionary的字符串)))(json)
和这个:
Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(json)
我尝试使用这个json:
{
/ pre>
Data:[
{
MCC:415,
F0:0,
REG ID:0
}
],
联系人:[
{
name:jocelyne,
mo:jocelyne
},
{
name:eliane,
mo:12345678
}
]
}
但这些都没有工作...
解决方案一种方法是创建一些与您收到的数据匹配的类:
类数据
公头作为标题
公共联系人作为列表(联系人)
结束类
< System.Runtime.Serialization.DataContract>
类标题
< System.Runtime.Serialization.DataMember(Name:=MCC)>
Public MCC As Integer
< System.Runtime.Serialization.DataMember(Name:=F0)>
Public F0 As Integer
< System.Runtime.Serialization.DataMember(Name:=REG ID)>
公共RegId As Integer
结束类
类联系
公共名称作为字符串
公共Mo作为字符串
结束类
所以很容易反序列化数据:
Sub Main
Dim json As String =< json>
{
标题:{
MCC:415,
F0:0,
REG ID:0
},
联系人:[
{
name:jocelyne,
mo:jocelyne
},
{
name:eliane,
mo:12345678
}
]
}< / json> .Value
Dim data As Data = JsonConvert.DeserializeObject(Of Data)(json)
data.Dump()
End Sub
现在你可以轻松地访问您要查找的值,例如
data.Header.MCC
等。
请注意,我使用
DataContract
/DataMember
这里是头文件
类,否则为解串器无法知道REG ID
应映射到RegId
(因为您不能拥有成员名称
如果你真的想要一个
DataTable
为你的联系人,只要声明Data.Contacts
asDataTable
:类数据
公头作为标题
公共联系人作为DataTable
结束类
i have the following json received through http post in asp.net:
{ "Header": { "MCC": "415", "F0": "0", "REG ID": "0" }, "Contacts": [ { "name": "jocelyne", "mo": "jocelyne" }, { "name": "eliane", "mo": "12345678" } ] }
i need to put only the data under contacts in a datatable and deserialize the data under header into 3 variables...
i tried this :
Dim deserializedProduct As List(Of Dictionary(Of String, String)) = JsonConvert.DeserializeObject(Of List(Of Dictionary(Of String, String)))(json)
and this:
Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(json)
i tried using this json:
{ "Data": [ { "MCC": "415", "F0": "0", "REG ID": "0" } ], "Contacts": [ { "name": "jocelyne", "mo": "jocelyne" }, { "name": "eliane", "mo": "12345678" } ] }
but none of these worked...
解决方案One way is to create some classes that matches the data you recieve:
Class Data Public Header As Header Public Contacts As List(Of Contact) End Class <System.Runtime.Serialization.DataContract> Class Header <System.Runtime.Serialization.DataMember(Name := "MCC")> Public MCC As Integer <System.Runtime.Serialization.DataMember(Name := "F0")> Public F0 As Integer <System.Runtime.Serialization.DataMember(Name := "REG ID")> Public RegId As Integer End Class Class Contact Public Name As String Public Mo As String End Class
So it's easy to deserialize the data:
Sub Main Dim json As String = <json> { "Header": { "MCC": "415", "F0": "0", "REG ID": "0" }, "Contacts": [ { "name": "jocelyne", "mo": "jocelyne" }, { "name": "eliane", "mo": "12345678" } ] }</json>.Value Dim data As Data = JsonConvert.DeserializeObject(Of Data)(json) data.Dump() End Sub
Now you can easily access the values you're looking for, like
data.Header.MCC
etc.Note that I use the
DataContract
/DataMember
here on theHeader
class because otherwise the deserializer has no way to know thatREG ID
should be mapped toRegId
(since you can't have member names with spaces in VB.Net).If you really want a
DataTable
for your contacts, just declareData.Contacts
asDataTable
:Class Data Public Header As Header Public Contacts As DataTable End Class
这篇关于将Json对象转换为数据和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!