0

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

1 Answer 1

3

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
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.