Using json.net from string to datatable in 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 creat开发者_StackOverflow中文版ed 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"
}
}
}
Thanks!
Raymond
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; }
}
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
);
}
精彩评论