json - 使用重复键解析 JSON (json-cpp)

标签 json jsoncpp

我正在使用 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/

相关文章:

json - Bada 2.0 JSON解析教程

c++ - 使用 JsonCPP 上的文本索引循环遍历 JSON 的所有项目

c++ - 将 jsoncpp 外部库添加到我的 qt 项目 : (symbol(s) not found for architecture x86_64)

json - 返回雅虎!使用YQL的摄氏温度天气API数据

javascript - 如何使用javascript从数组中填充无序列表?

jquery - 检查使用 JsonCpp 生成的 JSON 字符串

c++ - C++ 中的 Json : Parse a number as a string to avoid floating-point inaccuracy

java - 空回复正文发帖放心

python - 手动生成的 JSON 可以工作,但通过 json.dumps 创建的 JSON 不起作用,即使输出看起来完全相同

javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined