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"
    }
  }
}

Thanks!

Raymond

share|improve this question

1 Answer

up vote 3 down vote accepted

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
    );
}
share|improve this answer
Darin, i checked it and it works! Thanks! – Raymond Feb 18 '11 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.