简体   繁体   中英

How do I get a property from a list when parsing JSON in C# with JSON.NET?

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@gmail.com",
            "email@hotmail.com"
    ],
    "website":
    {
            "blog": "example.com"
    }
}

json2.json

{
"name": "Fname Lname",
"email": [
    "email@gmail.com",
    "email@hotmail.com"
],
"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

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);
}
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@gmail.com",       "email@hotmail.com"],"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);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM