【问题标题】:C# Parsing Strange JSON Response Content From API CallC# 解析来自 API 调用的奇怪 JSON 响应内容
【发布时间】:2021-04-29 15:22:03
【问题描述】:

我一直在拼命想弄清楚如何从我从 api 调用收到的这个 json 响应中解析我需要的数据。这是响应的开头:

{
    "response": {
        "results": [
            [
                1002,
                "Brent's Test Product o Fun",
                "TEST001",
                "1234567-",
                "100000000281",
                "12345678",
                "",
                "",
                true,
                "Brightpearl",
                "2020-06-18T13:38:01.000-05:00",
                "2021-04-27T07:45:24.000-05:00",
                439,
                null,
                74,
                1,
                "LIVE",
                null
            ],
            [
                1003,
                "Brent's Test Product 2",
                "TEST002",
                "",
                "",
                "",
                "",
                "",
                true,
                "Brightpearl",
                "2020-08-31T00:53:26.000-05:00",
                "2021-04-27T07:45:24.000-05:00",
                439,
                null,
                74,
                1,
                "LIVE",
                null
            ],
            [
                1004,
                "Brent's Test Product 3",
                "TEST003",
                "",
                "",
                "",
                "",
                "",
                true,
                "Brightpearl",
                "2020-09-16T00:30:55.000-05:00",
                "2021-04-27T07:45:24.000-05:00",
                439,
                null,
                74,
                1,
                "LIVE",
                null
            ],

我需要做的是遍历嵌套在“结果”中的节点。这些实际上是我需要使用的产品 skus。响应末尾返回了一堆元数据,我没有在此处包含。

我创建了这个类来处理结果。

public class SkuSearchResults
    {
        public int productId { get; set; }
        public string productName { get; set; }
        public string SKU { get; set; }
        public string barcode { get; set; }
        public string EAN { get; set; }
        public string UPC { get; set; }
        public string ISBN { get; set; }
        public string MPN { get; set; }
        public bool stockTracked { get; set; }
        public string salesChannelName { get; set; }
        public string createdOn { get; set; }
        public string updatedOn { get; set; }
        public int brightpearlCategoryCode { get; set; }
        public int productGroupId { get; set; }
        public int brandId { get; set; }
        public int productTypeId { get; set; }
        public string productStatus { get; set; }
        public int primarySupplierId { get; set; }

    }

【问题讨论】:

标签: c# json linq restsharp


【解决方案1】:

如果您尚未解决此问题,Brightpearl 的搜索端点会返回通用架构定义和原始结果,而不是 GET 端点返回的强类型结果。在不深入研究原因的情况下,您将完成解析元数据和原始结果并从中合成您的 c# 类的任务。幸运的是(像往常一样)Newtonsoft 将为您节省一天的时间。

创建一个将接收搜索结果的响应类

public class GenericSearchResult
{
    /// <summary>
    /// This holds all of the information about the fields returned in the results.
    /// </summary>
    [JsonProperty("metaData")]
    public SearchMetaData MetaData { get; set; }

    /// <summary>
    /// This is the raw data of the search request
    /// </summary>
    [JsonProperty("results")]
    public dynamic[] Results;
}

您可以使用 Postman 或类似工具来获取将在 MetaData 中返回的所有属性,但重要的是“columns”元素,它是返回的字段的数组以及它们将出现的顺序结果数组。

JsonConvert.Deserialize() 将您的 json 结果放入此构造中,然后您就可以进行手动转换了。

这些方面的东西应该让你开始

List<SkuSearchResults> results = new List<SkuSearchResults>();
foreach (dynamic row in searchResponse.Response.Results)
{
    //First, Create a JObject
    JObject obj = new JObject();
    int idx = 0;

    //Add the properties to the JObject
    foreach (MetaColumn column in searchResponse.Response.MetaData.Columns) 
    {
        obj.Add(new JProperty(column.Name, row[idx++]));
    }

    //Convert it to the target type
    results.Add(obj.ToObject<SkuSearchResults>());
}

如果您在不同的端点和结果类中进行大量搜索,您当然可以将其转换为通用函数 (T) 并合成任何内容。

   List<T> MakeObjectsFromSearch<T>(GenericSearchResponse searchResponse)

不要忘记添加一些空值处理,因为这些搜索经常为 int 字段返回空值(例如您的 primarySupplierId 和 productGroupId 属性)

希望对您有所帮助。肯定也让我头疼了一阵子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2017-06-14
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2021-04-27
    相关资源
    最近更新 更多