Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ASP.NET内核反序列化多维JSON数组_C#_Asp.net_Json_Razor Pages_System.text.json - Fatal编程技术网

C# 使用ASP.NET内核反序列化多维JSON数组

C# 使用ASP.NET内核反序列化多维JSON数组,c#,asp.net,json,razor-pages,system.text.json,C#,Asp.net,Json,Razor Pages,System.text.json,我已经试了几天来寻找这个问题,也许我遗漏了什么 在基于GitHub构建示例应用程序时,我遵循了Microsoft的建议: 不幸的是,这段代码似乎不适用于看起来是多维数组的对象 我的应用程序的希望是从我们的团队合作桌面帐户获取信息,以便我们可以使用ASP.NET应用程序创建报告 任何关于如何让它工作的提示或想法都会很好 这是我的密码: 文件夹(Models.TeamworkDesk)| Customer.cs using System; using System.Collections.Gene

我已经试了几天来寻找这个问题,也许我遗漏了什么

在基于GitHub构建示例应用程序时,我遵循了Microsoft的建议:

不幸的是,这段代码似乎不适用于看起来是多维数组的对象

我的应用程序的希望是从我们的团队合作桌面帐户获取信息,以便我们可以使用ASP.NET应用程序创建报告

任何关于如何让它工作的提示或想法都会很好

这是我的密码:

文件夹(Models.TeamworkDesk)| Customer.cs

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Vanilla.Models.TeamworkDesk
{
    public class Contact
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("value")]
        public string Value { get; set; }

        [JsonPropertyName("type")]
        public string Type { get; set; }

        [JsonPropertyName("isMain")]
        public bool IsMain { get; set; }

        [JsonPropertyName("createdAt")]
        public DateTime CreatedAt { get; set; }
    }

    public class Customer
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("timezoneId")]
        public int TimezoneId { get; set; }

        [JsonPropertyName("avatarURL")]
        public string AvatarURL { get; set; }

        [JsonPropertyName("email")]
        public string Email { get; set; }

        [JsonPropertyName("contacts")]
        public List<Contact> Contacts { get; set; }

        [JsonPropertyName("externalId")]
        public string ExternalId { get; set; }

        [JsonPropertyName("extraData")]
        public string ExtraData { get; set; }

        [JsonPropertyName("firstName")]
        public string FirstName { get; set; }

        [JsonPropertyName("jobTitle")]
        public string JobTitle { get; set; }

        [JsonPropertyName("language")]
        public string Language { get; set; }

        [JsonPropertyName("lastName")]
        public string LastName { get; set; }

        [JsonPropertyName("address")]
        public string Address { get; set; }

        [JsonPropertyName("mobile")]
        public string Mobile { get; set; }

        [JsonPropertyName("notes")]
        public string Notes { get; set; }

        [JsonPropertyName("organization")]
        public string Organization { get; set; }

        [JsonPropertyName("phone")]
        public string Phone { get; set; }

        [JsonPropertyName("trusted")]
        public bool Trusted { get; set; }

        [JsonPropertyName("welcomeEmailSent")]
        public bool WelcomeEmailSent { get; set; }

        [JsonPropertyName("facebookURL")]
        public string FacebookURL { get; set; }

        [JsonPropertyName("googlePlusURL")]
        public string GooglePlusURL { get; set; }

        [JsonPropertyName("linkedinURL")]
        public string LinkedinURL { get; set; }

        [JsonPropertyName("twitterHandle")]
        public string TwitterHandle { get; set; }

        [JsonPropertyName("createdAt")]
        public DateTime CreatedAt { get; set; }

        [JsonPropertyName("createdBy_users_id")]
        public int CreatedBy_Users_Id { get; set; }

        [JsonPropertyName("updatedAt")]
        public DateTime UpdatedAt { get; set; }

        [JsonPropertyName("updatedBy_users_id")]
        public int UpdatedBy_Users_Id { get; set; }

        [JsonPropertyName("company")]
        public object Company { get; set; }

        [JsonPropertyName("lastTicketDate")]
        public DateTime? LastTicketDate { get; set; }

        [JsonPropertyName("numTickets")]
        public int NumTickets { get; set; }
    }

    public class RootObject
    {
        [JsonPropertyName("customers")]
        public List<RootObject> Customers { get; set; }

        [JsonPropertyName("totalCustomers")]
        public int TotalCustomers { get; set; }
    }
}

谢谢。

您的Json很好,您刚刚混合了一些反序列化目标。使用您现有的代码,我将进行这些更改

在类
RootObject
中将
Customers
属性更改为类型
List

然后将反序列化行更改为:

Customers = await JsonSerializer.DeserializeAsync<RootObject>(responseStream);
Customers=wait JsonSerializer.DeserializeAsync(responseStream);

最后,作为个人偏好,我将
RootObject
改为
CustomerList

谢谢!我尝试了这些变化的一些组合,但不是所有的组合。我接受了您的建议,并更新了RootObject,因为它没有提供有意义的名称。
@page
@model Vanilla.Pages.TeamworkModel
@{
    ViewData["Title"] = "Teamwork Customers";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

@if (Model.GetCustomersError)
{
    <p>Unable to get customers from Teamwork. Please try again later.</p>
}
else
{
<ul>
    @foreach (var customer in Model.Customers)
    {
        <li>@customer.FirstName</li>
    }
</ul>
{
    "customers": [
        {
            "id": 94572,
            "timezoneId": 0,
            "avatarURL": "https://vanillacompany.teamwork.com/desk/images/examples/noPhoto1.png",
            "email": "testytester@vanillacompany.com",
            "contacts": [
                {
                    "id": 44783,
                    "value": "testytester@vanillacompany.com",
                    "type": "email",
                    "isMain": true,
                    "createdAt": "0001-01-01T00:00:00Z"
                }
            ],
            "externalId": "",
            "extraData": "",
            "firstName": "Testy",
            "jobTitle": "Code Tester",
            "language": "en",
            "lastName": "Tester",
            "address": "",
            "mobile": "",
            "notes": "",
            "organization": "Vanilla",
            "phone": "",
            "trusted": false,
            "welcomeEmailSent": false,
            "facebookURL": "",
            "googlePlusURL": "",
            "linkedinURL": "",
            "twitterHandle": "",
            "createdAt": "0001-01-01T00:00:00Z",
            "createdBy_users_id": 166240,
            "updatedAt": "0001-01-01T00:00:00Z",
            "updatedBy_users_id": 166240,
            "company": null,
            "lastTicketDate": null,
            "numTickets": 11
        },
        {
            "id": 85469,
            "timezoneId": 0,
            "avatarURL": "https://vanillacompany.teamwork.com/desk/images/examples/noPhoto2.png",
            "email": "codebreaker@vanillacompany.com",
            "contacts": [
                {
                    "id": 45002,
                    "value": "codebreaker@vanillacompany.com",
                    "type": "email",
                    "isMain": true,
                    "createdAt": "0001-01-01T00:00:00Z"
                }
            ],
            "externalId": "",
            "extraData": "",
            "firstName": "Code",
            "jobTitle": "",
            "language": "en",
            "lastName": "Breaker",
            "address": "",
            "mobile": "",
            "notes": "",
            "organization": "Vanilla",
            "phone": "",
            "trusted": false,
            "welcomeEmailSent": false,
            "facebookURL": "",
            "googlePlusURL": "",
            "linkedinURL": "",
            "twitterHandle": "",
            "createdAt": "0001-01-01T00:00:00Z",
            "createdBy_users_id": 166240,
            "updatedAt": "2016-05-04T20:46:10Z",
            "updatedBy_users_id": 166240,
            "company": null,
            "lastTicketDate": "2017-03-22T19:24:47Z",
            "numTickets": 5
        },
        {
            "id": 69421,
            "timezoneId": 0,
            "avatarURL": "https://vanillacompany.teamwork.com/desk/images/examples/noPhoto3.png",
            "email": "enduser@vanillacompany.com",
            "contacts": [
                {
                    "id": 45032,
                    "value": "enduser@vanillacompany.com",
                    "type": "email",
                    "isMain": true,
                    "createdAt": "2016-02-03T18:57:16Z"
                }
            ],
            "externalId": "",
            "extraData": "",
            "firstName": "End",
            "jobTitle": "",
            "language": "",
            "lastName": "User",
            "address": "",
            "mobile": "",
            "notes": "",
            "organization": "",
            "phone": "999-777-8888",
            "trusted": false,
            "welcomeEmailSent": false,
            "facebookURL": "",
            "googlePlusURL": "",
            "linkedinURL": "",
            "twitterHandle": "",
            "createdAt": "2016-02-03T18:57:16Z",
            "createdBy_users_id": 015874,
            "updatedAt": "2016-04-05T16:49:26Z",
            "updatedBy_users_id": 015874,
            "company": null,
            "lastTicketDate": "2016-04-05T16:48:27Z",
            "numTickets": 23
        },
    ],
    "totalCustomers": 1294
}
public List<Customer> Customers { get; set; }
public RootObject Customers { get; private set; }
Customers = await JsonSerializer.DeserializeAsync<RootObject>(responseStream);