如何使用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条评论
登录后参与评论

相关文章

来自分类Dev

使用JSON.NET解析JSON文件

来自分类Dev

asp.net使用json.net解析数组json

来自分类Dev

asp.net使用json.net解析数组json

来自分类Dev

如何使用GSON解析JSON文件

来自分类Dev

如何使用Javascript解析JSON文件

来自分类Dev

如何使用jQuery解析JSON文件?

来自分类Dev

如何使用JavaScript解析JSON文件?

来自分类Dev

如何使用jQuery解析JSON文件?

来自分类Dev

如何使用AngularJS解析许多json文件

来自分类Dev

如何解析JSON文件?

来自分类Dev

如何从文件解析这个json

来自分类Dev

使用BeautifulSoup解析JSON文件

来自分类Dev

使用AFNetworking解析JSON文件

来自分类Dev

使用BeautifulSoup解析JSON文件

来自分类Dev

使用python解析json文件

来自分类Dev

使用ASP.NET C#解析JSON中的数组

来自分类Dev

VB.NET如何解析结果并将其添加到json文件的列表中?

来自分类Dev

尝试使用 Swifty JSON 解析 json 文件

来自分类Dev

使用Json.Net解析嵌套的Json

来自分类Dev

使用JSON.NET解析JSON数据

来自分类Dev

使用Json.net解析JSON数组

来自分类Dev

使用Json.Net解析嵌套的Json

来自分类Dev

使用 JSON.NET 解析多级 JSON

来自分类Dev

使用 Json.Net 解析 JSON 数组

来自分类Dev

如何使用 ASP.NET MVC 将 JSON 数据存储和更新到文件中?

来自分类Dev

如何使用JSON.NET解析JSON子级?

来自分类Dev

如何使用JSON.Net解析此JSON?

来自分类Dev

在asp.net中解析Json对象

来自分类Dev

在ASP.NET中解析JSON数组