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.
-
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.M.Babcock– M.Babcock2013年03月06日 05:52:11 +00:00Commented Mar 6, 2013 at 5:52
-
1Note: My code sample below removes the extra braces (present in the question text) that surround the array. they cause the deserialize operation to fail.Glenn Ferrie– Glenn Ferrie2013年03月06日 06:00:17 +00:00Commented 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.yiqun– yiqun2013年03月06日 06:04:12 +00:00Commented Mar 6, 2013 at 6:04
1 Answer 1
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
answered Mar 6, 2013 at 5:55
Glenn Ferrie
10.4k3 gold badges46 silver badges77 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-cs