简体   繁体   English

使用Boost ptree将JSON数组解析为std :: string

[英]Parse JSON array as std::string with Boost ptree

I have this code that I need to parse/or get the JSON array as std::string to be used in the app. 我有这个代码,我需要解析/或获取JSON数组作为应用程序中使用的std :: string。

std::string ss = "{ \"id\" : \"123\", \"number\" : \"456\", \"stuff\" : [{ \"name\" : \"test\" }] }";

ptree pt2;
std::istringstream is(ss);
read_json(is, pt2);
std::string id = pt2.get<std::string>("id");
std::string num= pt2.get<std::string>("number");
std::string stuff = pt2.get<std::string>("stuff"); 

What is needed is the "stuff" to be retrieved like this as std::string [{ "name" : "test" }] 需要的是像std :: string [{ "name" : "test" }]这样检索的“东西”

However the code above stuff is just returning empty string. 但是上面的代码stuff只是返回空字符串。 What could be wrong 可能有什么不对

Arrays are represented as child nodes with many "" keys: 数组表示为具有许多""键的子节点:

docs 文档

  • JSON arrays are mapped to nodes. JSON数组映射到节点。 Each element is a child node with an empty name. 每个元素都是一个空名称的子节点。 If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation. 如果节点同时具有命名和未命名子节点,则无法将其映射到JSON表示。

Live On Coliru 住在Coliru

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;

int main() {
    std::string ss = "{ \"id\" : \"123\", \"number\" : \"456\", \"stuff\" : [{ \"name\" : \"test\" }, { \"name\" : \"some\" }, { \"name\" : \"stuffs\" }] }";

    ptree pt;
    std::istringstream is(ss);
    read_json(is, pt);

    std::cout << "id:     " << pt.get<std::string>("id") << "\n";
    std::cout << "number: " << pt.get<std::string>("number") << "\n";
    for (auto& e : pt.get_child("stuff")) {
        std::cout << "stuff name: " << e.second.get<std::string>("name") << "\n";
    }
}

Prints 打印

id:     123
number: 456
stuff name: test
stuff name: some
stuff name: stuffs

continue with @sehe 's answer, related to the question asked by @xkm 继续@sehe的回答,与@xkm提出的问题有关

Is it possible to get it like '[{ "name" : "some" }, { "name" : "stuffs" }]' 有可能让它像'[{“name”:“some”},{“name”:“stuffs”}]'

Yes, you can. 是的你可以。 Just treat it with an "unnamed" key which means the key with empty string. 只需用“未命名”键来处理它,这意味着键为空字符串。

fg FG

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <iostream>

using boost::property_tree::ptree;

int main()
{
    std::stringstream ss;
    ss << R"([{"a": 5}, {"a": 9}])";

    ptree pt;
    read_json(ss, pt);
    for (auto& item : pt.get_child(""))
       std::cout << "value is " << item.second.get<int>("a") << std::endl;
}

Since "stuff" is an array, you can iterate over the elements of it, which are dictionaries. 由于"stuff"是一个数组,你可以遍历它的元素,它们是字典。 And then you can iterate over the elements of the dictionary, which are key-value pairs: 然后你可以迭代字典的元素,它们是键值对:

for (const auto& dict : pt2.get_child("stuff")) {
    for (const auto& kv : dict.second) {
        std::cout << "key = " << kv.first << std::endl;
        std::cout << "val = " << kv.second.get_value<std::string>() << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM