C++ 如何在C++;
在下面的示例中,如何获取C++ 如何在C++;,c++,json,rapidjson,C++,Json,Rapidjson,在下面的示例中,如何获取名称和余额 { "user": { "Name": "John", "Balance": "2000.53" } } 我不太了解Rapidjson,只知道它是C++中解析JSON的第三方库。但是我想说,为什么不使用boost来解决这个问题呢。给你我的密码,它完美地解决了你的问题 在运行我的代码之前,请安装boost库。强烈推荐 #include <boost/property_tree/json_parser.hpp
名称
和余额
{
"user": {
"Name": "John",
"Balance": "2000.53"
}
}
我不太了解Rapidjson,只知道它是C++中解析JSON的第三方库。但是我想说,为什么不使用boost来解决这个问题呢。给你我的密码,它完美地解决了你的问题 在运行我的代码之前,请安装boost库。强烈推荐
#include <boost/property_tree/json_parser.hpp>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
boost::property_tree::ptree parser;
const string str = "{ \"user\": { \"Name\": \"John\", \"Balance\": \"2000.53\" } }";
stringstream ss(str);
boost::property_tree::json_parser::read_json(ss, parser);
//get "user"
boost::property_tree::ptree user_array = parser.get_child("user");
//get "Name"
const string name = user_array.get<string>("Name");
//get "Balance"
const string balance = user_array.get<string>("Balance");
cout << name << ' ' << balance << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
boost::property_tree::ptree解析器;
常量字符串str=“{\'user\':{\'Name\':\'John\',\'Balance\':\'2000.53\'}”;
stringstream ss(str);
boost::property_tree::json_parser::read_json(ss,parser);
//获取“用户”
boost::property_tree::ptree user_array=parser.get_child(“user”);
//获取“名称”
const string name=user_array.get(“name”);
//获得“平衡”
const string balance=user_array.get(“balance”);
不能容易
来吧,至少试试
rapidjson::Document doc;
doc.Parse(str);
const Value& user = doc["user"];
string name = user["Name"].GetString();
string balance = user["Balance"].GetString();