【问题标题】:Why isn't the JSON object an array?为什么 JSON 对象不是数组?
【发布时间】:2012-07-07 02:07:02
【问题描述】:

我的 JSON 文件如下所示:

{
"strings": {
    "keyone": "abc",
    "keytwo": "def",
    }
}

使用 C++,

const Json::Value strings = root["strings"];
for (int index = 0; index < strings.size(); index++) 
{
    std::cout << strings.isArray() << std::endl;
    std::cout << strings.get(index, "ERROR") << std::endl;
}

strings.IsArray() 返回 0,表示它不是数组。而第二行strings.get(index, "ERROR)",只是在执行时崩溃,可能是因为我不是像数组一样使用它。

所以我假设字符串只是一个字符串而不是数组。我怎样才能使它成为一个数组对象?

【问题讨论】:

  • 你使用的是什么 JSON 库?
  • 为什么上面的 JSON 包含一个 JSON 数组? JSON 类型(数组、对象、字符串等)都是不同的,并且不“共享”祖先(与 JavaScript 数组和 JavaScript 对象不同)。因此,大多数实现倾向于不允许将 JSON-Object 视为 JSON-Array(这样做实际上会违反此类结构的 JSON 保证)。
  • @AndréCaron 我正在使用 JsonCpp。

标签: c++ json arrays


【解决方案1】:

“strings.IsArray()”返回0,表示不是数组

当然可以,"strings":{ ... } 是 JSON 术语中的“对象”。

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 2014-02-24
    • 2020-09-08
    • 2011-10-06
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多