-2

I have received the following json at my client application:

[{
    "ErrorCode" : 0,
    "ErrorMessage" : "The operation completed successfully."
}, {
    "configured" : true,
    "id" : "abc"
}]

Properties ErrorCode and ErrorMessage belong to the ErrorInfo-class and the properties configured and id belong to the Data-class.

I need to deserialize this JSON so that I can populate these two classes ErrorInfo and Data. How can I deserialize this json code to C#?

Sebastiaan M
  • 5,775
  • 2
  • 27
  • 28
Pooja Kuntal
  • 461
  • 1
  • 4
  • 13
  • I'd use Json.NET... do you already have a class representing the objects? Do you want to access them dynamically? Have you tried anything yourself yet? – Jon Skeet Aug 03 '15 at 09:37
  • Please edit the detail into the question - after reading the duplicate. – Jon Skeet Aug 03 '15 at 09:40
  • its not duplicate .. it is kind of array of jsons which belongs to different classes.. – Pooja Kuntal Aug 03 '15 at 09:45
  • So how do you expect anything to know which class to deserialize which object as? It sounds like you should probably use LINQ to JSON instead. – Jon Skeet Aug 03 '15 at 09:47
  • i know .. the first json corresponds to ErrorInfo and second to Data. I am just trying a way out – Pooja Kuntal Aug 03 '15 at 09:48
  • Yes, *you* know that - but how are you expecting to communicate that to anything else? And how were *we* meant to know that from your original question? (And is it Session or Data? You're being inconsistent...) – Jon Skeet Aug 03 '15 at 09:49

1 Answers1

1

You can install the NuGet packet Newtonsoft And use the class JsonConvert. An example:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

Make also a class Movie whit the same properties and types.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144