-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"
        }
    ]
}
6
  • 8
    Deserialize this JSON first into some readable object and then you can iterate over its length. Commented May 11, 2015 at 8:59
  • Check this: http://stackoverflow.com/questions/12676746/parse-json-string-in-c-sharp Commented May 11, 2015 at 9: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,....? Commented May 11, 2015 at 9: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... Commented May 11, 2015 at 9:01
  • I get this data using dictionary. internal static Dictionary<string, object> 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.. Commented May 11, 2015 at 9:22

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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<MyClass>. If you didn't want to use Json.Net, there is also a JavaScriptSerializer in System.Web.Extensions (msdn.microsoft.com/en-us/library/…). Have upvoted this answer as @Coder1409 is correct.
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
foreach (KeyValuePair<string, object> entry in App.userData) { if (entry.Key == "mnu") { //this.MenuItems.Add({branchtext = Value of a }) } }
foreach (KeyValuePair<string, object> entry in App.userData) { if (entry.Key == "list") { //this.MenuItems.Add({branchtext = Value of a }) } }
what are you trying to do here ??

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.