如何使用 C# 处理 Cosmos db 中的 Json 对象数组

问题描述 投票:0回答:1

我正在开发一个 API,它只是在具有以下 JSON 正文的文档中添加一条记录。

我的问题是

Status
在 CosmosDB 中始终显示为 null。

我确信我没有正确处理“状态”的类定义。

Status
是一个对象数组。谁能解释一下我做错了什么?

Json 体:

{
    "name": "John", 
    "Status": [
        {
            "Status": "Closed",
            "StatusUpdatedDateTime": "2023-10-01T00:00:00Z",
            "StatusUpdatedBy": "XXX"
        }
    ]
}

类定义 -

TopObject
类:

public class TopObject
{
     public string name { get; set; }
     public List<Status> status { get; set; }
}

public class Status
{
    public string Status { get; set; }  
    public DateTimeOffset StatusUpdatedDateTime { get; set; }
    public string StatusUpdatedBy { get; set; }
}
c# asp.net-mvc azure-cosmosdb
1个回答
0
投票

我同意@madreflection,你不能有一个与班级同名的班级成员。使用

[JsonProperty("Status")]
并为该属性指定除“Status”之外的任何名称(小写“status”很好,尽管不是属性的常用大小写)。您可以使用如下定义类:

public class TopObject
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("Status")]
    public List<Status> Status { get; set; }
}
public class Status
{
    [JsonProperty("Status")]
    public string status { get; set; }

    [JsonProperty("StatusUpdatedDateTime")]
    public DateTimeOffset StatusUpdatedDateTime { get; set; }

    [JsonProperty("StatusUpdatedBy")]
    public string StatusUpdatedBy { get; set; }
}

然后就可以成功读取数据了,如下图:

enter image description here

以下是完整代码供您参考:

using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class TopObject
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("Status")]
    public List<Status> Status { get; set; }
}
public class Status
{
    [JsonProperty("Status")]
    public string status { get; set; }

    [JsonProperty("StatusUpdatedDateTime")]
    public DateTimeOffset StatusUpdatedDateTime { get; set; }

    [JsonProperty("StatusUpdatedBy")]
    public string StatusUpdatedBy { get; set; }
}

public class Program
{
    private static CosmosClient cosmosClient;
    private static Container container;
    private static string endpointUri = "<endpoint>";
    private static string primaryKey = "<primaryKey>";
    private static string databaseId = "<dbName>";
    private static string containerId = "<containerName>";

    public static async Task Main(string[] args)
    {
        cosmosClient = new CosmosClient(endpointUri, primaryKey);
        container = cosmosClient.GetContainer(databaseId, containerId);
        string documentId = "<id>";
        var topObject = await GetDocumentByIdAsync(documentId);
        Console.WriteLine("Name: " + topObject.Name);
        foreach (var status in topObject.Status)
        {
            Console.WriteLine("Status: " + status.status);
            Console.WriteLine("Updated Date: " + status.StatusUpdatedDateTime);
            Console.WriteLine("Updated By: " + status.StatusUpdatedBy);
        }

    }
    public static async Task<TopObject> GetDocumentByIdAsync(string documentId)
    {
        try
        {
            ItemResponse<TopObject> response = await container.ReadItemAsync<TopObject>(documentId, new PartitionKey(documentId));
            return response.Resource;
        }
        catch (CosmosException ex)
        {
            Console.WriteLine($"Error: {ex.StatusCode}, {ex.Message}");
            return null;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.