我是json的新手,请帮帮我!假设我有以下json对象
{
"Table": "",
"Id": "",
"Column": [
{
"ColumnText": "",
"ColumnSqlName": ""
},
{
"ColumnText": "",
"ColumnSqlName": ""
}
]
}
我想用asp.net反序列化上面的json,最好的方法是什么?感谢!!!
答案 0 :(得分:2)
我建议使用JSON.NET库将对象序列化和反序列化为json
例如:它将json对象反序列化为C#object ...
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
答案 1 :(得分:1)
MSDN有一个简单的示例供您遵循。如果它是一个服务,可能更容易使用WCF简单地使用它。
答案 2 :(得分:1)
您可以使用内置类JavaScriptSerializer
。 MyClass
是与json结构匹配的类
var serializer = new JavaScriptSerializer();
MyClass instance = serializer.Deserialize<MyClass>(myJson);
答案 3 :(得分: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)
感谢您的所有回复,但上述代码可能效果很好。