在C#中从字符串到数据表使用json.net [英] Using json.net from string to datatable in C#
本文介绍了在C#中从字符串到数据表使用json.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
希望有人可以帮我举一个例子,因为我是JSON的新手: 从网络服务中,我收到一个JSON字符串.我了解它是从数据表创建的. 如何在C#中管理将其反序列化为数据集?也许有人对我有用.
Hopefully can someone help me with an example, because I'm new in JSON: From a webservice I receive a JSON string. I understand it is created from a datatable. How do I manage in C# to Deserialize this to a dataset? Maybe someone has something for me.
{
"DataToJohnson": {
"0": {
"maat_id": "1",
"maat": "11"
},
"1": {
"maat_id": "2",
"maat": "11+"
},
"2": {
"maat_id": "3",
"maat": "12+"
},
"3": {
"maat_id": "4",
"maat": "12/13"
}
}
}
谢谢!
Raymond
推荐答案
您可以定义一个表示该JSON数据的模型:
You could define a model that will represent this JSON data:
public class Data
{
public int Maat_id { get; set; }
public string Maat { get; set; }
}
public class MyModel
{
public Dictionary<int, Data> DataToJohnson { get; set; }
}
,然后使用 Json.NET 将字符串反序列化为模型
and then use Json.NET to deserialize this string to the model
var json =
@"{
""DataToJohnson"": {
""0"": {
""maat_id"": ""1"",
""maat"": ""11""
},
""1"": {
""maat_id"": ""2"",
""maat"": ""11+""
},
""2"": {
""maat_id"": ""3"",
""maat"": ""12+""
},
""3"": {
""maat_id"": ""4"",
""maat"": ""12/13""
}
}
}";
MyModel model = JsonConvert.DeserializeObject<MyModel>(json);
foreach (var item in model.DataToJohnson)
{
Console.WriteLine(
"id: {0}, maat_id: {1}, maat: {2}",
item.Key, item.Value.Maat_id, item.Value.Maat
);
}
这篇关于在C#中从字符串到数据表使用json.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文