【问题标题】:Json Object into a datatable and a string将 Json 对象转换为数据表和字符串
【发布时间】:2013-11-01 08:44:38
【问题描述】:

我通过 http post 在 asp.net 中收到了以下 json:

 {
"Header": {
    "MCC": "415",
    "F0": "0",
    "REG ID": "0" 
},
"Contacts": [
    {
        "name": "jocelyne",
        "mo": "jocelyne"
    },
    {
        "name": "eliane",
        "mo": "12345678"
    }
]
}

我只需要将联系人下的数据放入数据表中,并将标题下的数据反序列化为3个变量...

我试过这个:

 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:

 {
"Data": [
    {
        "MCC": "415",
        "F0": "0",
        "REG ID": "0"
    }
],
"Contacts": [
    {
        "name": "jocelyne",
        "mo": "jocelyne"
    },
    {
        "name": "eliane",
        "mo": "12345678"
    }
]
}

但这些都不起作用......

【问题讨论】:

    标签: asp.net json vb.net datatables http-post


    【解决方案1】:

    一种方法是创建一些与您收到的数据相匹配的类:

    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
    

    所以反序列化数据很容易:

    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 等。

    请注意,我在Header 类上使用了DataContract/DataMember,否则反序列化程序无法知道REG ID 应该映射到RegId(因为你不能拥有成员VB.Net 中带空格的名称)。

    如果你真的想为你的联系人使用DataTable,只需将Data.Contacts 声明为DataTable

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

    【讨论】:

    • 我知道当前位置没有可用的源代码
    • @JocelyneElKhoury 此错误与此代码无关。尝试谷歌搜索no source code available for the current location 以查找有关此错误的大量信息。也许你在 Visual Studio 的调试设置上摆弄?
    猜你喜欢
    • 2018-05-01
    • 2019-08-27
    • 2020-05-27
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2011-04-23
    • 2012-06-14
    • 2012-02-20
    相关资源
    最近更新 更多