<bdo id='9XkMC'></bdo><ul id='9XkMC'></ul>
    1. <small id='9XkMC'></small><noframes id='9XkMC'>

      1. <i id='9XkMC'><tr id='9XkMC'><dt id='9XkMC'><q id='9XkMC'><span id='9XkMC'><b id='9XkMC'><form id='9XkMC'><ins id='9XkMC'></ins><ul id='9XkMC'></ul><sub id='9XkMC'></sub></form><legend id='9XkMC'></legend><bdo id='9XkMC'><pre id='9XkMC'><center id='9XkMC'></center></pre></bdo></b><th id='9XkMC'></th></span></q></dt></tr></i><div id='9XkMC'><tfoot id='9XkMC'></tfoot><dl id='9XkMC'><fieldset id='9XkMC'></fieldset></dl></div>

        <legend id='9XkMC'><style id='9XkMC'><dir id='9XkMC'><q id='9XkMC'></q></dir></style></legend>
        <tfoot id='9XkMC'></tfoot>

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

        Parse JSON array as std::string with Boost ptree(使用 Boost ptree 将 JSON 数组解析为 std::string)
                <tbody id='apKTW'></tbody>

              <small id='apKTW'></small><noframes id='apKTW'>

            • <tfoot id='apKTW'></tfoot>

                <bdo id='apKTW'></bdo><ul id='apKTW'></ul>
                <i id='apKTW'><tr id='apKTW'><dt id='apKTW'><q id='apKTW'><span id='apKTW'><b id='apKTW'><form id='apKTW'><ins id='apKTW'></ins><ul id='apKTW'></ul><sub id='apKTW'></sub></form><legend id='apKTW'></legend><bdo id='apKTW'><pre id='apKTW'><center id='apKTW'></center></pre></bdo></b><th id='apKTW'></th></span></q></dt></tr></i><div id='apKTW'><tfoot id='apKTW'></tfoot><dl id='apKTW'><fieldset id='apKTW'></fieldset></dl></div>

                  <legend id='apKTW'><style id='apKTW'><dir id='apKTW'><q id='apKTW'></q></dir></style></legend>

                • 本文介绍了使用 Boost ptree 将 JSON 数组解析为 std::string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这个代码,我需要解析/或获取 JSON 数组作为 std::string 以在应用程序中使用.

                  I have this code that I need to parse/or get the JSON array as std::string to be used in the app.

                  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"); 
                  

                  需要的是像这样检索东西"作为 std::string [{ "name" : "test" }]

                  What is needed is the "stuff" to be retrieved like this as std::string [{ "name" : "test" }]

                  然而,stuff 上面的代码只是返回空字符串.可能有什么问题

                  However the code above stuff is just returning empty string. What could be wrong

                  推荐答案

                  数组表示为具有许多 "" 键的子节点:

                  Arrays are represented as child nodes with many "" keys:

                  docs

                  • JSON 数组映射到节点.每个元素都是一个名称为空的子节点.如果一个节点同时具有命名和未命名的子节点,则它无法映射到 JSON 表示.

                  生活在 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") << "
                  ";
                      std::cout << "number: " << pt.get<std::string>("number") << "
                  ";
                      for (auto& e : pt.get_child("stuff")) {
                          std::cout << "stuff name: " << e.second.get<std::string>("name") << "
                  ";
                      }
                  }
                  

                  印刷品

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

                  这篇关于使用 Boost ptree 将 JSON 数组解析为 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)

                    <bdo id='z3YzQ'></bdo><ul id='z3YzQ'></ul>
                  • <tfoot id='z3YzQ'></tfoot>
                          <tbody id='z3YzQ'></tbody>

                          <i id='z3YzQ'><tr id='z3YzQ'><dt id='z3YzQ'><q id='z3YzQ'><span id='z3YzQ'><b id='z3YzQ'><form id='z3YzQ'><ins id='z3YzQ'></ins><ul id='z3YzQ'></ul><sub id='z3YzQ'></sub></form><legend id='z3YzQ'></legend><bdo id='z3YzQ'><pre id='z3YzQ'><center id='z3YzQ'></center></pre></bdo></b><th id='z3YzQ'></th></span></q></dt></tr></i><div id='z3YzQ'><tfoot id='z3YzQ'></tfoot><dl id='z3YzQ'><fieldset id='z3YzQ'></fieldset></dl></div>
                          <legend id='z3YzQ'><style id='z3YzQ'><dir id='z3YzQ'><q id='z3YzQ'></q></dir></style></legend>
                        1. <small id='z3YzQ'></small><noframes id='z3YzQ'>