1

I'm able to parse simple properties using JSON.NET with this C# code:

Code C#

    WebClient c = new WebClient();
    var data = c.DownloadString("http://localhost/json1.json");
    JObject o = JObject.Parse(data);
    listBox1.Items.Add(o["name"]);
    listBox1.Items.Add(o["email"][0]);
    listBox1.Items.Add(o["email"][1]);
    listBox1.Items.Add(o["website"]["blog"]);

json1.json

{
    "name": "Fname Lname",
    "email": [
            "[email protected]",
            "[email protected]"
    ],
    "website":
    {
            "blog": "example.com"
    }
}

json2.json

{
"name": "Fname Lname",
"email": [
    "[email protected]",
    "[email protected]"
],
"website":
{
    "blog": "example.com"
},
"faculty":
{
    "department": [
    {
        "name": "department.name", 
        "location": "department.location"
    }
    ]
}
}


From the second JSON file, I'm not able to get name and location from the department. How do I do that in C#?

  • name : department.name
  • location: department.location
3
  • 3
    Have you tried o['faculty']['department'][0]['name']? Commented Dec 12, 2013 at 16:57
  • 1
    you can probably use the Debugger to learn what the fields of your dematerialized object contain. Commented Dec 12, 2013 at 16:58
  • :D Thank you so much @George Stocker Commented Dec 12, 2013 at 17:03

2 Answers 2

1
yourjsonobject.faculty.department[0].name;
yourjsonobject.faculty.department[0].location;

Here is some jsfiddle to help you with this:

http://jsfiddle.net/sCCrJ/

 var r = JSON.parse('{"name": "Fname Lname","email": [    "[email protected]",       "[email protected]"],"website":{    "blog": "example.com"},"faculty":{    "department": [    {        "name": "department.name",         "location": "department.location"    }    ]}}');
alert(r.faculty.department[0].name);
alert(r.faculty.department[0].location);

   for (var i = 0; i < r.faculty.department.length; i++) {
       alert(r.faculty.department[i].name);
   }
Sign up to request clarification or add additional context in comments.

2 Comments

The OP asked for it in C#; not JavaScript.
Yeah, I think it is easier to understand the core concept with jsFiddle snippet. Perhaps, this will be teaching him "how to fish", I suppose :) .
0

Your problem is that department is an array of objects (though it happens to just contain one item here), but you're not accessing it like it is. You can use o["faculty"]["department"][0]["name"] to get your data.

You might want to use classes (here are ones auto-converted with http://json2csharp.com/) to more easily work with your data.

public class Website
{
    public string blog { get; set; }
}

public class Department
{
    public string name { get; set; }
    public string location { get; set; }
}

public class Faculty
{
    public List<Department> department { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public List<string> email { get; set; }
    public Website website { get; set; }
    public Faculty faculty { get; set; }
}

Then you can get all of the data (instead of hoping the fixed indexes are right, and that you didn't make a typo in the property names) with this code:

WebClient c = new WebClient();
var data = c.DownloadString("http://localhost/json1.json");
var o = JsonConvert.DeserializeObject<RootObject>(data);

listBox1.Items.Add(o.name);
foreach (var emailAddr in o.email)
    listBox1.Items.Add(emailAddr);
listBox1.Items.Add(o.website.blog);
foreach (var dept in o.faculty.department)
{
    listBox1.Items.Add(dept.name);
    listBox1.Items.Add(dept.location);
}

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.