0

How do I deserialize following JSON string to an object?

{
    "ArrayOfResults": {
        "Results": [
            {
                "ErrorID": "0",
                "ErrorMessage": null,
                "MetroID": "281",
                "MetroName": "050908 add metor no dffd"
            },
            {
                "ErrorID": "0",
                "ErrorMessage": null,
                "MetroID": "284",
                "MetroName": "050908 added with dff"
            }
        ]
    }
}
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
EagleEye
  • 23
  • 3
  • I think this was answered in a previous post: http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – Elixir Nov 28 '11 at 08:26
  • Please format and indent your code properly next time. If you would do that, you noticed you were missing a closing square bracket, so it wasn't a valid JSON. – David Ferenczy Rogožan Feb 23 '16 at 01:07

3 Answers3

0

You can use this framework: http://json.codeplex.com/

Taken from documentation:

JObject o = new JObject(
  new JProperty("Name", "John Smith"),
  new JProperty("BirthDate", new DateTime(1983, 3, 20))
  );

JsonSerializer serializer = new JsonSerializer();
Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person));

Console.WriteLine(p.Name);
// John Smith
Francois
  • 10,730
  • 7
  • 47
  • 80
0

What you have shown is invalid JSON. You will need to fix your JSON in order to hope to handle it with a JSON serializer:

{
    "ArrayOfResults": {
        "Results": [
            {
                "ErrorID": "0",
                "ErrorMessage": null,
                "MetroID": "281",
                "MetroName": "050908 add metor no dffd"
            },
            {
                "ErrorID": "0",
                "ErrorMessage": null,
                "MetroID": "284",
                "MetroName": "050908 added with dff"
            }
        ]
    }
}

Once you have fixed it you can use the JavaScriptSerializer class to deserialize it to strongly typed model that you define:

public class Item
{
    public int ErrorID { get; set; }
    public string ErrorMessage { get; set; }
    public int MetroID { get; set; }
    public string MetroName { get; set; }
}

public class ResultArray
{
    public Item[] Results { get; set; }
}

public class Result
{
    public ResultArray ArrayOfResults { get; set; }
}

class Program
{
    static void Main()
    {
        var json = @"{ ""ArrayOfResults"": { ""Results"": [ {""ErrorID"": ""0"", ""ErrorMessage"": null, ""MetroID"": ""281"", ""MetroName"": ""050908 add metor no dffd"" }, {""ErrorID"": ""0"", ""ErrorMessage"": null, ""MetroID"": ""284"", ""MetroName"": ""050908 added with dff"" }]}}";
        var serializer = new JavaScriptSerializer();
        var response = serializer.Deserialize<Result>(json);

        // TODO: do something with the response
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Your help is really appreciate able ,Can i get rid of "Result" and "ResultArray" class.i just want to create only "item" class and deserilized json . – EagleEye Nov 28 '11 at 08:51
0
var s = new JavaScriptSerializer();
var result = s.Deserialize<List<YourType>>(strJson);

?

And of course it would be better to serialize the list of your type similar way

idm
  • 1,728
  • 2
  • 19
  • 26