我正在使用 JsonCpp v0.6.0 来解析以下 JSON 字符串:
{
"3.7":"de305d54-75b4-431b-adb2-eb6b9e546011",
"3.7":"de305d54-75b4-431b-adb2-eb6b9e546012",
"3.8":"de305d54-75b4-431b-adb2-eb6b9e546013"
}
如下:
Json::Value root;
Json::Reader reader;
// value contains the JSON string
if (!reader.parse(value, root, false))
{
// parse error
}
在调用parse
之后,root
包含映射中的两个条目:
[0] first = "3.7", second = "de305d54-75b4-431b-adb2-eb6b9e546012",
[1] first = "3.8", second = "de305d54-75b4-431b-adb2-eb6b9e546013",
即第一个 JSON 记录已被第二个覆盖。没有错误报告。
这种行为是预期的吗?这是正确的吗?
我以为可能是报错了,提示JSON字符串中存在重复键
最佳答案
喜欢JSON RFC遗憾的是对象名称(键)应该是唯一的。
The names within an object SHOULD be unique.
RFC 还定义了如果它们不是,则行为是不可预测的。
请参阅 RFC 中的这段引文:
An object whose names are all unique is interoperable in the sense
that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not
unique, the behavior of software that receives such an object is
unpredictable. Many implementations report the last name/value pair
only. Other implementations report an error or fail to parse the
object, and some implementations report all of the name/value pairs,
including duplicates.
关于json - 使用重复键解析 JSON (json-cpp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480121/