如何使用C ++解析json数组? [英] How can i parse json arrays with C++?
本文介绍了如何使用C ++解析json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
std::stringstream ss;
ss << "{ \"values\": \"A\": 1, \"B\": 10 }";
我想在下面将此流设置为这种格式.
I'd like to make this stream into this format below.
{
"values": [
{ "A": 1, "B": 10 }
...
]
}
有人知道如何使用c ++和boost ptree解析数组的值吗?
Does anybody know how to parse the values of array using c++ and boost ptree?
推荐答案
假设输入 input.json
这是使用Boost Spirit Qi的一种简单方法:
Here's a simplistic method using Boost Spirit Qi:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <fstream>
#include <map>
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
std::istream& operator>>(std::istream& is, Array& into) {
using namespace boost::spirit::qi;
using it = boost::spirit::istream_iterator;
rule<it, std::string()> s;
rule<it, Element(), space_type> r, e;
s = '"' >> ~char_('"') >> '"';
r = (s >> ':' >> int_) % ',';
e = '{' >> r >> '}';
return is >> phrase_match('{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into);
}
}
int main() {
std::ifstream ifs("input.json");
ifs.unsetf(std::ios::skipws);
Array array;
if (ifs >> array) {
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "\n--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} else {
std::cout << "Parsing failed\n";
}
}
打印
std::istream& {anonymous}::operator>>(std::istream&, {anonymous}::Array&)
Parsed 13 elements:
--------------------
{ "A": 1, "B": 10, }
--------------------
{ "C": 3, "D": 12, }
--------------------
{ "E": 5, "F": 14, }
--------------------
{ "G": 7, "H": 16, }
--------------------
{ "I": 9, "J": 18, }
--------------------
{ "K": 11, "L": 20, }
--------------------
{ "M": 13, "N": 22, }
--------------------
{ "O": 15, "P": 24, }
--------------------
{ "Q": 17, "R": 26, }
--------------------
{ "S": 19, "T": 28, }
--------------------
{ "U": 21, "V": 30, }
--------------------
{ "W": 23, "X": 32, }
--------------------
{ "Y": 25, "Z": 34, }
使用Spirit X3
同一笔交易:
Using Spirit X3
Same deal:
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
namespace parser {
using namespace boost::spirit::x3;
rule<struct rule_key_t, std::string> s;
rule<struct rule_element_t, Element> r;
rule<struct rule_braced_t, Element> e;
auto s_def = '"' >> ~char_('"') >> '"';
auto r_def = (s >> ':' >> int_) % ',';
auto e_def = '{' >> r >> '}';
BOOST_SPIRIT_DEFINE(s,r,e)
}
std::istream& operator>>(std::istream& is, Array& into) {
using namespace parser;
boost::spirit::istream_iterator f(is), l;
if (!phrase_parse(f, l, '{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into))
{
is.setstate(is.rdstate() | std::ios::failbit);
}
return is;
}
}
具有相同main()
这有点不同,我选择不实施operator>>
,因为Boost Property并没有真正负担得起.
This is somewhat difference, I opted to not implement operator>>
because Boost Property doesn't really afford that.
#include <boost/property_tree/json_parser.hpp>
#include <fstream>
#include <iostream>
#include <map>
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
Array read(std::string fname) {
std::ifstream ifs(fname);
Array into;
using namespace boost::property_tree;
ptree pt;
read_json(ifs, pt);
for (auto& entry : pt.get_child("values")) {
Element e;
for (auto& property : entry.second)
e[property.first] = property.second.get_value(0);
into.push_back(std::move(e));
}
return into;
}
}
int main() {
try {
auto array = read("input.json");
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} catch (...) {
std::cout << "Parsing failed\n";
}
}
当然,输出还是与以前相同.
Of course, the output is, again, the same as earlier.
这篇关于如何使用C ++解析json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文