c# - 验证包含字典的 JSON 对象

标签 c# json validation json.net

我正在为 .NET 项目中的许多对象使用 JSON 签名,然后验证这些对象以确保它们包含正确数量的参数等。

但是,我不知道如何验证这个对象,因为“myActions”可以包含不同数量的元素。这个例子只有两个,但可以有更多,甚至只有一个(实际上,它总是有 1 到 5 个元素)。

{
    "myCanvas": {
        "width": 700,
        "height": 700,
        "initialScene": "scene1"
    },
    "myActions": {
        "actionOne": {
            "type": "web",
            "text": "Open our homepage.",
            "params": {
                "linkUri": "http://your.server.name/"
            }
        },
        "actionTwo": {
            "type": "web",
            "text": "Show item.",
            "params": {
                "linkUri": "http://your.server.name/items/123"
            }
        }
    },
    "otherStuff": {
        "controlBoard": {
            "format": [
            {
                "image": "image1",
                "x": 0,
                "y": 0,
                "w": 700,
                "h": 700
            }
            ]
        }
    }
}

我该如何验证这一点,因为 JSON 签名可以采用五种形式之一?我可以将其称为字典并以这种方式对其进行验证吗?现在,我正在使用类似这样的东西(如下)w/a String.Format(因此所有额外的引号和大括号),但我在 5 个几乎相同的对象中使用这个相同的巨大签名,因为我知道 myActions 将包含 1 到 5 个元素。请记住,我无法控制顶部 JSON block 的格式;我只写验证器(如下所示)。如果“myActions”是一个数组,这将是小菜一碟。

""myMetadata"":{{   
    ""myCanvas"":{{
        ""type"":""object"",
        ""required"":true,
        ""properties"":{{
            ""width"":{{
                ""type"":""number"",
                ""required"":true
            }},
            ""height"":{{
                ""type"":""number"",
                ""required"":true
            }},
            ""initialScene"":{{
                ""type"":""string"",
                ""required"":true
            }}
        }}
    }},
    ""myActions"":{{
        ""type"":""object"",
        ""required"":true,
        ""properties"":{{
            {0}
        }}
    }},
    ""otherStuff"":{{
        ""type"":""object"",
        ""required"":true,
        ""properties"":{{
            ""controlBoard"":{{
                ""type"":""object"",
                ""required"":true,
                ""properties"":{{
                    ""format"":{{  
                        ""type"":""array"",
                        ""items"":{{  
                            ""type"":""object"",
                            ""properties"":{{  
                                ""image"":{{  
                                    ""type"":""string"",
                                }},    
                                ""x"":{{  
                                    ""type"":""number"",
                                }},
                                ""y"":{{  
                                    ""type"":""number"",
                                }},
                                ""w"":{{  
                                    ""type"":""number"",
                                }},
                                ""h"":{{  
                                    ""type"":""number"",
                                }}
                            }}
                        }}
                    }}
                }}
            }}
        }}
    }}
}};

最佳答案

是的,这是可行的。一些 JSON 解析器/反序列化器确实支持几种不同的语法,以便反序列化为强类型字典,如您的示例(即您的第一个 JSON 片段)。

我知道我有。

将数据映射到 .NET 字典的最常见语法包括:

要么,

"SomeDictionary": [
    { "Key": "Key1", "Value": "Value1" },
    { "Key": "Key2", "Value": "Value2" },
    ...
 ]

或者,更直接地说,如您的情况:

 "SomeDictionary": {
     "Key1": "Value1",
     "Key2": "Value2",
     ...
  }

(其中“Value1”、“Value2”等不需要是原始类型,也可以是对象或列表/数组等)

您的目标对象模型(即其类)因此看起来类似于:

public class MyParams
{
    public string linkUri { get; set; }
    // ...
}

public class MyAction
{
    public string type { get; set; }
    public string text { get; set; }
    public MyParams @params { get; set; }
}

public class MyOuterObject
{
    // other properties omitted
    public IDictionary<string, MyAction> myActions { get; set; }
    // ...
}

例如,参见:

(示例对象模型)

https://github.com/ysharplanguage/FastJsonParser/blob/master/JsonTest/ParserTests.cs#L123

(对应单元测试)

https://github.com/ysharplanguage/FastJsonParser/blob/master/JsonTest/ParserTests.cs#L729

'希望这有帮助,

关于c# - 验证包含字典的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30587001/

相关文章:

javascript - JSON 数组 - 显示数据所需的帮助

json - 使用 Go 将 JSON 日志重复存储为文件并确保文件名唯一

c# - 使用类型名称将 JSON 文本反序列化为特定对象类型

ruby-on-rails-3 - Rails 3 针对一列验证一个值的唯一性?

javascript - 关闭模式框时验证错误消息保留在页面上

java - 在 JSF 中使用 h :inputFile and javax. servlet.http.Part 上传之前验证文件

c# - 如何删除计数最低的子列表并保留主列表中计数最高的子列表?

c# - Response.End() 异常;

c# - 无法创建新 View ,因为尚未创建主窗口

c# - Web API 方法返回 JSON 数据