2

我对json很陌生,请帮帮我!假设我有以下 json 对象

{
    "Table": "",
    "Id": "",
    "Column": [
        {
            "ColumnText": "",
            "ColumnSqlName": ""
        },
        {
            "ColumnText": "",
            "ColumnSqlName": ""
        }
    ]
}

我想使用asp.net来反序列化上面的json,最好的方法是什么?谢谢!!!

4

4 回答 4

2

我建议使用JSON.NET库将对象序列化和反序列化为 json

例如:它将 json 对象反序列化为 C# 对象...

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}";

JObject o = JObject.Parse(json);

string name = (string)o["Name"];
// Apple

JArray sizes = (JArray)o["Sizes"];

string smallest = (string)sizes[0];
// Small

在此处输入图像描述

于 2012-06-06T12:24:54.800 回答
1

MSDN有一个简单的示例供您学习。如果它是一项服务,那么使用 WCF 简单地使用它可能会更容易。

于 2012-06-06T12:25:11.320 回答
1

您可以使用内置类JavaScriptSerializerthis。MyClass是一个匹配json结构的类

var serializer = new JavaScriptSerializer();
MyClass instance = serializer.Deserialize<MyClass>(myJson);
于 2012-06-06T12:38:03.033 回答
1
    Public Class JsonObj
    Private _Table As String
    Public Property Table() As String
        Get
            Return _Table
        End Get
        Set(ByVal value As String)
            _Table = value
        End Set
    End Property
    Private _Id As String
    Public Property Id() As String
        Get
            Return _Id
        End Get
        Set(ByVal value As String)
            _Id = value
        End Set
    End Property

    Private _Column As JsonSetting()
    Public Property Column() As JsonSetting()
        Get
            Return _Column
        End Get
        Set(ByVal value As JsonSetting())
            _Column = value
        End Set
    End Property
End Class

Public Class JsonSetting
    Private _ColumnText As String
    Public Property ColumnText() As String
        Get
            Return _ColumnText
        End Get
        Set(ByVal value As String)
            _ColumnText = value
        End Set
    End Property

    Private _ColumnSqlName As String
    Public Property ColumnSqlName() As String
        Get
            Return _ColumnSqlName
        End Get
        Set(ByVal value As String)
            _ColumnSqlName = value
        End Set
    End Property
End Class

用法:

Dim o As New JavaScriptSerializer
Dim instance As JsonObj = o.Deserialize(Of JsonObj)(json_str)

感谢您的所有回复,但上面的代码可能会很好用。

于 2012-06-07T03:53:06.093 回答