【问题标题】:JSON response with varying nesting pattern from 3rd-party API来自 3rd-party API 的具有不同嵌套模式的 JSON 响应
【发布时间】:2021-11-13 10:23:05
【问题描述】:

我收到一个包含多个嵌套属性的第三方 API 响应:

变体 1:

{
    "Property1": {
        "Property2A": {
            "Key1": "1",
            "Key2": "2",
            "Key3": "3"
        },
        "Property2B": {
            "Property3": {
                "Key4": "A",
                "Key5": "B",
                "Key6": "C",
                "Property4": {
                    "Property5": {
                        "Property6": [
                            {
                                "Key7": "1",
                                "Property7A": {
                                    "X": "1"
                                },
                                "Property7B": {
                                    "X": "2"
                                },
                                "Property7C": {
                                    "X": "3"
                                }
                            },
                            {
                                "Property7D": {
                                    "X": "INeedThisString"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

我只需要值“INeedThisString”。

我能够通过合适的模型结构(通过将模型映射到 Json 文件生成)并使用以下声明来达到属性 "X": "INeedThisString" 的值:

Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(MyJsonString);
string result = obj.Property1.Property2B.Property3.Property4.Property5.Property6[1].Property7D.X;

这是我的问题

API 有时会发布此架构的变体,唯一的区别是 Property3 声明为数组,例如:

变体 2:

{
    "Property1": {
        "Property2A": {
            "Key1": "1",
            "Key2": "2",
            "Key3": "3"
        },
        "Property2B": {
            "Property3": [ //<-----
                    {
                    "Key4": "A",
                    "Key5": "B",
                    "Key6": "C",
                    "Property4": {
                        "Property5": {
                            "Property6": [
                                {
                                    "Key7": "1",
                                    "Property7A": {
                                        "X": "1"
                                    },
                                    "Property7B": {
                                        "X": "2"
                                    },
                                    "Property7C": {
                                        "X": "3"
                                    }
                                },
                                {
                                    "Property7D": {
                                        "X": "INeedThisString"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]  //<-----
        }
    }
}

//&lt;-----:添加 2x 用于说明目的。

显然,变体 1 和我当前的模型结构并未将 Property3 声明为数组。

问题:

什么是解决这个问题的优雅方法在预处理中不接触 Json(删除/替换)

我是否应该实现一组替代模型并通过错误函数在这两个模型集之间切换?请注意,7A-7D 属性中的键都是相同的:"X"

【问题讨论】:

    标签: asp.net json asp.net5


    【解决方案1】:

    我当前的工作解决方案涵盖了解析所需的一组替代类,由映射 Json 响应的变体 2 产生。

    所有类都通过使用指令using Newtonsoft.Json 由 Json 标头 [JsonProperty("actual-string-shown-in-Json")] 修饰。

    两个替代的JsonConvert 函数被放置在一个try-catch 语句中:

    string result;
    try
    {
    Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(MyJsonString);
    result = obj.Property1.Property2B.Property3.Property4.Property5.Property6[1].Property7D.X;
    }
    
    catch
    {
    AltRootobject obj = JsonConvert.DeserializeObject<AltRootobject>(MyJsonString);
    result = obj.AltProperty1.AltProperty.AltProperty[0].AltProperty.AltProperty5.AltProperty6[1].AltProperty7D.AltX;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-06
      • 2016-03-22
      • 2020-09-07
      • 2017-07-30
      • 1970-01-01
      • 2021-05-31
      • 2020-02-14
      • 2016-09-03
      相关资源
      最近更新 更多