如何用asp.net解析Json文件

时间:2015-01-11 10:55:57

标签: c# json parsing

我有Json文件的问题,我不知道如何在attribut中获取attribut,这是我的json文件的格式

    {
"people" : [
{
    "url": "\/alfresco\/service\/api\/people\/guest",
    "userName": "guest",
    "enabled": false,
    "firstName": "Guest",
    "lastName": "",
    "jobtitle": null,
    "organization": null,
    "organizationId": "", 
    "location": null,
    "telephone": null,
    "mobile": null,
    "email": "",
    "companyaddress1": null,
    "companyaddress2": null,
    "companyaddress3": null,
    "companypostcode": null,
    "companytelephone": null,
    "companyfax": null,
    "companyemail": null,
    "skype": null,
    "instantmsg": null,
    "userStatus": null,
    "userStatusTime": null,
    "googleusername": null,
    "quota": -1,
    "sizeCurrent": 0,
    "emailFeedDisabled": false,
    "persondescription": null
}
        ,
{
    "url": "\/alfresco\/service\/api\/people\/mjackson",
    "userName": "mjackson",
    "enabled": false,
    "avatar": "api\/node\/workspace\/SpacesStore\/519ebedc-0827-4fba-a8e3-c51e39385e0c\/content\/thumbnails\/avatar",
    "firstName": "Mike",
    "lastName": "Jackson",
    "jobtitle": "Web Site Manager",
    "organization": "Green Energy",
    "organizationId": null, 
    "location": "Threepwood, UK",
    "telephone": "012211331100",
    "mobile": "012211331100",
    "email": "mjackson@example.com",
    "companyaddress1": "100 Cavendish Street",
    "companyaddress2": "Threepwood",
    "companyaddress3": "UK",
    "companypostcode": "ALF1 SAM1",
    "companytelephone": "",
    "companyfax": "",
    "companyemail": "",
    "skype": "mjackson",
    "instantmsg": "",
    "userStatus": "Working on a new web design for the corporate site",
    "userStatusTime": { "iso8601": "2011-02-15T21:13:09.649+01:00"},
    "googleusername": "",
    "quota": -1,
    "sizeCurrent": 8834773,
    "emailFeedDisabled": false,
    "persondescription": "Mike is a demo user for the sample Alfresco Team site."
}
        ,


]
}

我想仅获取userName attribut的值

这是我的代码:

 WebClient c = new WebClient();
                     var data = c.DownloadString(url2);
                     Console.WriteLine(data);
                     JObject o = JObject.Parse(data);
                     Response.Write("Name: " + o["people"]); //here i want to get ["people"]["userName"]

PS:对不起我的英文

2 个答案:

答案 0 :(得分:0)

People是一个数组,而不是包含用户名的对象。

将此模型添加到项目中的文件中:

    public class Rootobject
{
    public Person[] people { get; set; }
}

public class Person
{
    public string url { get; set; }
    public string userName { get; set; }
    public bool enabled { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string jobtitle { get; set; }
    public string organization { get; set; }
    public string organizationId { get; set; }
    public string location { get; set; }
    public string telephone { get; set; }
    public string mobile { get; set; }
    public string email { get; set; }
    public string companyaddress1 { get; set; }
    public string companyaddress2 { get; set; }
    public string companyaddress3 { get; set; }
    public string companypostcode { get; set; }
    public string companytelephone { get; set; }
    public string companyfax { get; set; }
    public string companyemail { get; set; }
    public string skype { get; set; }
    public string instantmsg { get; set; }
    public string userStatus { get; set; }
    public Userstatustime userStatusTime { get; set; }
    public string googleusername { get; set; }
    public int quota { get; set; }
    public int sizeCurrent { get; set; }
    public bool emailFeedDisabled { get; set; }
    public string persondescription { get; set; }
    public string avatar { get; set; }
}

public class Userstatustime
{
    public DateTime iso8601 { get; set; }
}

然后像这样解析用户名:

var json = JsonConvert.DeserializeObject<Rootobject>(data);

foreach (var person in json.people) {
    Console.WriteLine("Name: " + person.userName);
}

答案 1 :(得分:0)

在您的模型类更改中:

public int quota { get; set; }

为:

public long quota {get; set;}