JSON对象变成一个DataTable和一个字符串 [英] Json Object into a datatable and a string

查看:129
本文介绍了JSON对象变成一个DataTable和一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过HTTP POST收到asp.net以下JSON:

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"
    }
]
}

我只需要把数据下接触,一个DataTable和反序列化的数据在头为3个变量...

i need to put only the data under contacts in a datatable and deserialize the data under header into 3 variables...

我想这:

 Dim deserializedProduct As List(Of Dictionary(Of String, String)) = JsonConvert.DeserializeObject(Of List(Of Dictionary(Of String, String)))(json)

和这样的:

Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(json)

我尝试使用此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

现在,你可以轻松地访问你正在寻找的值,比如 data.Header.MCC 等。

Now you can easily access the values you're looking for, like data.Header.MCC etc.

请注意,我用的是 DataContract / 数据成员这里的标题类,否则解串器没有办法知道 REG ID 应该被映射到 REGID (自你不能有在VB.Net空格)成员的名称。

Note that I use the DataContract/DataMember here on the Header class because otherwise the deserializer has no way to know that REG ID should be mapped to RegId (since you can't have member names with spaces in VB.Net).

如果你真的想要一个数据表为您的联系人,只是声明 Data.Contacts 数据表

If you really want a DataTable for your contacts, just declare Data.Contacts as DataTable:

Class Data
    Public Header As Header
    Public Contacts As DataTable
End Class

这篇关于JSON对象变成一个DataTable和一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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