-1

I have this following json. I want to iterate over it and get the values of a and b so that i could set it at other place. Can anyone please tell me how to do this in c#? json is --

{
    "_id": "12345",
    "r": [
        "my-user",
        "college-student"
    ],
    "list": [
        {
            "a": "CSE",
            "b": "DataBase"
        },
        {
            "a": "IT",
            "b": "ComputerNetwork"
        }
    ]
}
w.b
  • 11,026
  • 5
  • 30
  • 49
anonyms
  • 11
  • 1
  • 8
    Deserialize this JSON first into some readable object and then you can iterate over its length. – Dhrumil May 11 '15 at 08:59
  • Check this: [http://stackoverflow.com/questions/12676746/parse-json-string-in-c-sharp](http://stackoverflow.com/questions/12676746/parse-json-string-in-c-sharp) – Fabio May 11 '15 at 09:00
  • @anonyms is a and b always "only" in the list part? And additionally HOW d you want the objects back as a list of strings,....? – Thomas May 11 '15 at 09:00
  • Note that you don't need a custom class for this - JSON.Net's LINQ to JSON is fine for this sort of thing. Of course, if you want to represent the objects for anything else, it could definitely be *useful* to create a class... – Jon Skeet May 11 '15 at 09:01
  • I get this data using dictionary. internal static Dictionary userData = null; i make some http call and userdata is the required json. i am using Newtonsoft for json conversion but no luck till now.. – anonyms May 11 '15 at 09:22
  • @anonyms if you do that (using an dictionary, ...) then mention it in your question else its hard to help if you only show half of what you are really doing – Thomas May 11 '15 at 09:28

1 Answers1

3

Install json.net from nuget and use this code to get a and b

        dynamic res = JObject.Parse(json);
        List<dynamic> ls =JsonConvert.DeserializeObject<List<dynamic>>( res.list.ToString());
        ls.ForEach(item=> Console.WriteLine(item.a+" "+item.b));

and since we love one liners just do the following

List<dynamic> res = JsonConvert.DeserializeObject<List<dynamic>>(((dynamic)(JObject.Parse(json))).list.ToString());

if you'd like to generate the class for the json just use this website http://json2csharp.com/

the class you would get is

public class List
{
    public string a { get; set; }
    public string b { get; set; }
}

public class RootObject
{
    public string _id { get; set; }
    public List<string> r { get; set; }
    public List<List> list { get; set; }
}

and deserialize by using :

var res = JsonConvert.DeserializeObject<RootObject>(json);
Coder1409
  • 523
  • 4
  • 12
  • If you know the structure of the data coming in, you can make a corresponding C# class and deserialise to that to save using dynamic. i.e. JsonConvert.DeserializeObject. If you didn't want to use Json.Net, there is also a JavaScriptSerializer in System.Web.Extensions (https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx). Have upvoted this answer as @Coder1409 is correct. – JsAndDotNet May 11 '15 at 09:19
  • He is just looking for the values of a and b to be saved somewhere else i don't see the importance of using a class here , either way i will add the class solution – Coder1409 May 11 '15 at 09:25
  • foreach (KeyValuePair entry in App.userData) { if (entry.Key == "mnu") { //this.MenuItems.Add({branchtext = Value of a }) } } – anonyms May 11 '15 at 09:41
  • foreach (KeyValuePair entry in App.userData) { if (entry.Key == "list") { //this.MenuItems.Add({branchtext = Value of a }) } } – anonyms May 11 '15 at 09:42
  • what are you trying to do here ?? – Coder1409 May 11 '15 at 09:46