使用升压/ property_tree获取值从JSON文件,具有多元素/阵列/子阵C ++ [英] Getting values from a json file using boost/property_tree, with multiple elements/arrays/sub-arrays C++
问题描述
的事实:
- 我使用VS2013和用C ++开发。
- 我使用升压API从标准/合法的JSON文件获取值。
- 我不能提取的 name4.js 的 name5.js 的和的 name6.js 的名称。
- 我已经找遍了计算器/谷歌并没有找到我一起工作的下方JSON文件任何解释。
JSON文件
{
背景: {
脚本:[name1.js,name2.js,name3.js]
},
default_popup:popup.html,
default_title:__MSG_name__,
content_scripts:[{
all_frames:真实,
JS:[name4.js,name5.js,name6.js],
match_about_blank:真实,
匹配:HTTP:// * / *,https://开头* / *]
run_at:document_start
},{
all_frames:真实,
JS:include.postload.js],
match_about_blank:真实,
匹配:HTTP:// * / *,https://开头* / *]
run_at:document_end
}]
}
什么工作:
-
正如你可以在下面的code看到的,我能提取name1.js,name2.js和name3.js使用background.scripts(本例为提升网站):
的boost :: property_tree :: ptree中的文档;
提高:: property_tree :: read_json(C:/温度\\\\的manifest.jsonDOC);
的std ::矢量<串GT; jsFiles;
尝试{
BOOST_FOREACH(升压:: property_tree :: ptree中:: VALUE_TYPE&放大器; framePair,doc.get_child(background.scripts)){
jsFiles.push_back(framePair.second.data());
}
}
赶上(升压::例外常量和放大器;除息){}
什么不工作:
- 我要提取的JS名称这是其他地区:
- name4.js
- name5.js
- name6.js
-
我无法得到它使用下面的code的工作:
BOOST_FOREACH(升压:: property_tree :: ptree中:: VALUE_TYPE&放大器; framePair2,doc.get_child(content_scripts)){
jsFiles.push_back(framePair2.second.data());
我得到的是,
在矢量字符串。
我甚至尝试 jsFiles.push_back(framePair2.second.get<标准::字符串>(JS));
,但它仍然无法正常工作<。 / p>
我也试过其他的方法,但没有成功。
我倒是AP preciate如果我能得到一个工作code,因为我的想法。
获取LT;标准::字符串&GT;(JS)
不行,因为JS
有一个数组值。
为(自动急症:pt.get_child(content_scripts))
为(自动&安培; R:e.second.get_child(JS))
性病::法院LT&;&LT; r.second.get_value&LT;标准::字符串&GT;()&LT;&LT; \\ n;
应该做的。
<大骨节病> 住在Coliru 骨节病>
的#include&LT; sstream&GT;
#包括LT&;升压/ property_tree / ptree.hpp&GT;
#包括LT&;升压/ property_tree / json_parser.hpp&GT;
#包括LT&;&iostream的GT;标准::字符串常量样品= R(
{
背景: {
脚本:[name1.js,name2.js,name3.js]
},
default_popup:popup.html,
default_title:__MSG_name__,
content_scripts:[{
all_frames:真实,
JS:[name4.js,name5.js,name6.js],
match_about_blank:真实,
匹配:HTTP:// * / *,https://开头* / *]
run_at:document_start
},{
all_frames:真实,
JS:include.postload.js],
match_about_blank:真实,
匹配:HTTP:// * / *,https://开头* / *]
run_at:document_end
}]
});使用boost :: property_tree :: ptree中;
命名空间J =提振:: property_tree :: json_parser;诠释主(){
的std :: istringstream ISS(样品);
ptree中PT;
Ĵ:: read_json(ISS,PT); 为(自动急症:pt.get_child(content_scripts))
为(自动&安培; R:e.second.get_child(JS))
性病::法院LT&;&LT; r.second.get_value&LT;标准::字符串&GT;()&LT;&LT; \\ n;
}
打印
name4.js
name5.js
name6.js
include.postload.js
The facts:
- I am using VS2013 and developing in C++.
- I am using boost API to get the values from a standard/legit json file.
- I can't extract name4.js, name5.js and name6.js names.
- I have searched all over stackoverflow/Google and didn't find any explanation for the below json file I am working with.
json file:
{
"background": {
"scripts": [ "name1.js", "name2.js", "name3.js" ]
},
"default_popup": "popup.html",
"default_title": "__MSG_name__",
"content_scripts": [ {
"all_frames": true,
"js": [ "name4.js", "name5.js", "name6.js" ],
"match_about_blank": true,
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_start"
}, {
"all_frames": true,
"js": [ "include.postload.js" ],
"match_about_blank": true,
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_end"
} ]
}
What works:
As you can see in the code below, I was able to extract "name1.js", "name2.js" and "name3.js" using "background.scripts" (the example at boost website):
boost::property_tree::ptree doc; boost::property_tree::read_json("C:/Temp\\manifest.json", doc); std::vector<string> jsFiles; try{ BOOST_FOREACH(boost::property_tree::ptree::value_type& framePair, doc.get_child("background.scripts")){ jsFiles.push_back(framePair.second.data()); } } catch (boost::exception const &ex){}
What doesn't work:
- I want to extract the rest of the js names which are:
- name4.js
- name5.js
- name6.js
I couldn't get it to work using the below code:
BOOST_FOREACH(boost::property_tree::ptree::value_type& framePair2, doc.get_child("content_scripts")){ jsFiles.push_back(framePair2.second.data());
What I get is ""
in the vector string.
I even tried jsFiles.push_back(framePair2.second.get<std::string>("js"));
but it still doesn't work.
I have also tried other methods with no success.
I'd appreciate if I could get a working code because I am out of ideas.
get<std::string>("js")
can't work because "js"
has an array value.
for(auto& e : pt.get_child("content_scripts"))
for(auto& r : e.second.get_child("js"))
std::cout << r.second.get_value<std::string>() << "\n";
should do
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
std::string const sample = R"(
{
"background": {
"scripts": [ "name1.js", "name2.js", "name3.js" ]
},
"default_popup": "popup.html",
"default_title": "__MSG_name__",
"content_scripts": [ {
"all_frames": true,
"js": [ "name4.js", "name5.js", "name6.js" ],
"match_about_blank": true,
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_start"
}, {
"all_frames": true,
"js": [ "include.postload.js" ],
"match_about_blank": true,
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_end"
} ]
})";
using boost::property_tree::ptree;
namespace j = boost::property_tree::json_parser;
int main() {
std::istringstream iss(sample);
ptree pt;
j::read_json(iss, pt);
for(auto& e : pt.get_child("content_scripts"))
for(auto& r : e.second.get_child("js"))
std::cout << r.second.get_value<std::string>() << "\n";
}
Prints
name4.js
name5.js
name6.js
include.postload.js
这篇关于使用升压/ property_tree获取值从JSON文件,具有多元素/阵列/子阵C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!