如何使用asp.net解析Json文件

卡迪

我的Json文件有问题,我不知道如何在属性内获取属性,这是我的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": "[email protected]",
    "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属性的值

这是我的代码:

 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:对不起,我的英语

埃米尔·佩尔斯(Emile Pels)

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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章