Does JSON syntax allow duplicate keys in an object?

后端 未结 12 1911
青春惊慌失措
青春惊慌失措 2020-11-22 00:05

Is this valid json?

{
    \"a\" : \"x\",
    \"a\" : \"y\"
}

http://jsonlint.com/ says yes.

http://www.json.org/ doesn\'t say anyth

12条回答
  •  逝去的感伤
    2020-11-22 00:10

    In C# if you deserialise to a Dictionary it takes the last key value pair:

    string json = @"{""a"": ""x"", ""a"": ""y""}";
    var d = JsonConvert.DeserializeObject>(json);
    // { "a" : "y" }
    

    if you try to deserialise to

    class Foo
    {
        [JsonProperty("a")]
        public string Bar { get; set; }
    
        [JsonProperty("a")]
        public string Baz { get; set; }
    }
    
    var f = JsonConvert.DeserializeObject(json);
    

    you get a Newtonsoft.Json.JsonSerializationException exception.

提交回复
热议问题