-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#?

6
  • 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? Commented Aug 3, 2015 at 9:37
  • Please edit the detail into the question - after reading the duplicate. Commented Aug 3, 2015 at 9:40
  • its not duplicate .. it is kind of array of jsons which belongs to different classes.. Commented Aug 3, 2015 at 9: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. Commented Aug 3, 2015 at 9:47
  • i know .. the first json corresponds to ErrorInfo and second to Data. I am just trying a way out Commented Aug 3, 2015 at 9:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.