5

I have a string like the following in C#. I tried with JSON.NET but couldn't figure out how to retrieve the value.

"{[{'Name':'AAA','Age':'22','Job':'PPP'},
{'Name':'BBB','Age':'25','Job':'QQQ'},
{'Name':'CCC','Age':'38','Job':'RRR'}]}";

I would like

foreach (user in users){
   Messagebox.show(user.Name,user.Age)
}

Any help will be greatly appreciated.

3
  • Have you read the documentation for the library or tried anything yourself? There are a dozen or so tutorials online that should be able to help you get started. Commented Mar 6, 2013 at 5:52
  • 1
    Note: My code sample below removes the extra braces (present in the question text) that surround the array. they cause the deserialize operation to fail. Commented Mar 6, 2013 at 6:00
  • @M.Babcock Yes.I try to use Dataset in json.net but there is no root in my json string.I google examples them always with root. Commented Mar 6, 2013 at 6:04

1 Answer 1

10

Here is a code sample:

class Program
{
    static void Main(string[] args)
    {
        var text = @"[{'Name':'AAA','Age':'22','Job':'PPP'},
                    {'Name':'BBB','Age':'25','Job':'QQQ'},
                    {'Name':'CCC','Age':'38','Job':'RRR'}]";

        dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
        for (var i = 0; i < data.Count; i++)
        {
            dynamic item = data[i];
            Console.WriteLine("Name: {0}, Age: {1}", (string)item.Name, (string)item.Age);
        }

        Console.ReadLine();
    }
}

I downloaded Json.Net through NuGet, but otherwise this is a standard .NET 4.0 Console App

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.