【问题标题】:How to parse a json having nested json type objects with boost如何使用 boost 解析具有嵌套 json 类型对象的 json
【发布时间】:2017-07-06 14:44:59
【问题描述】:

我有一个 json,看起来像

{
  "children": [
    {
      "name": "apple",
      "path": "hutchison",
      "state": 16,
      "url": "https://go.gl/",
      "id": "ZXNrb0luZGlh",
      "properties": [
        {
          "id": "type",
          "name": "type",
          "propertyType": "string",
          "value": "COMPANY"
        },
        {
          "id": "folderify",
          "name": "folderify",
          "propertyType": "boolean",
          "value": "true"
        },
        {
          "id": "name",
          "name": "name",
          "propertyType": "string",
          "value": "yum"
        }
      ],
      "content": [],
      "createdBy": "00ui3tqahuu6bwMhU0i6",
      "creationDate": 1497348318103,
      "modificationDate": 1499082955407,
      "modifiedBy": "00ui3tqahuu6bwMhU0i6"
    },
    {
      "name": "test",
      "path": "test",
      "state": 13,
      "url": "https://ODE/v0/test",
      "id": "YW5ib3Rlc3Q=",
      "properties": [
        {
          "id": "string",
          "name": "string",
          "propertyType": "string",
          "value": "string"
        }
      ],
      "content": [],
      "createdBy": "00u33355JQXgmzqO90i5",
      "creationDate": 1498463285568,
      "modificationDate": 1498463356176,
      "modifiedBy": "00u33355JQXgmzqO90i5"
    },
    {
      "name": "KE",
      "path": "KE",
      "state": 4,
      "url": "https://full.com/NODE/v0/DEKE",
      "id": "REVLRQ==",
      "properties": [
        {
          "id": "type",
          "name": "type",
          "propertyType": "string",
          "value": "COMPANY"
        },
        {
          "id": "folderify",
          "name": "folderify",
          "propertyType": "boolean",
          "value": "true"
        },
        {
          "id": "name",
          "name": "name",
          "propertyType": "string",
          "value": "KE"
        }
      ],
      "content": [],
      "createdBy": "00uy9bswhaVnUggF00i6",
      "creationDate": 1498805345347,
      "modificationDate": 1498805346371,
      "modifiedBy": "00uy9bswhaVnUggF00i6"
    }
]
,
  "name": "",
  "path": "",
  "state": 27,
  "url": "https://go.gl/ODE/v0",
  "id": "ROOT",
  "properties": [],
  "content": [],
  "createdBy": "INITIAL",
  "creationDate": 1497261853581,
  "modificationDate": 1498805345347,
  "modifiedBy": "00uy9bswhaVnUggF00i6"
}

我当然希望使用 boost 库(也欢迎 cpp 中的任何其他方法)遍历每个键值对,以便稍后将它们保存在相关容器中。

【问题讨论】:

  • 然后呢?你试过什么吗?请发布您的代码以及您遇到的问题。我们不能只为您编写代码!
  • 我想要两片饼干和一块饼干,但我附近没有肯德基。

标签: c++ json boost


【解决方案1】:

以下代码 sn-p 遍历每个密钥对并将其存储在相关集合中:

#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;

int main() {
    ptree pt;
    std::ifstream ifs("input.txt");
    read_json(ifs, pt);
}

集合形成一棵树。值得注意的是,属性树。这可能是也可能不是您需要的,因为您的目标没有给出。

【讨论】:

    【解决方案2】:

    我实际上是在使用Json for modern C++ 来管理任何 Json。它非常直观且易于集成。

    我真的不知道您要达到哪个键值,但假设您想将第一个孩子的名称存储在字符串变量中。

    你会像这样解析你的 json:

    json yourJson = json::parse(yourJsonString);
    

    然后您将访问该名称并将其存储在一个字符串变量中,如下所示:

    string nameFirstChildren = yourJson["children"][0]["name"].get<string>();
    

    然后,您可以将其保存在您选择的容器中,或者您可以在将其保存到容器之前不使用字符串变量来执行此操作。

    任何对你有用的东西!

    你也可以使用RapidJson

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多