利用jsoncpp库生成和解析json字符串

jsoncpp库的使用:

1.生成json字符串方法1:借助value和writer

string uid_str = "10000911";
string account = "vip1";
string json_str;
Json::Value value;
Json::FastWriter writer;
value["uid"] = uid_str;
value["account"] = account;

json_str = writer.write(value);

2.生成json字符串方法2:直接赋值

string json_msg = "{\"ret\":0,\"msg\":\"success\"}";//需要转义双引号,冒号无需转义

3.解析json字符串

string jsonStr = "{\"uid\":101,\"msg\":\"charge_in_game\"}";
Json::value value;
if(!Json::FromString(value,jsonStr))
{
//error log
//return false;
}
if(!value.isObject())
{
//error log
//return false;
}
unsigned uid = 0;
string msg;
if(!Json::GetUInt(value,"uid",uid))
{
//error log
//return false;
}
if(!Json::GetString(value,"msg",msg))
{
//error log
//return false;
}

你可能感兴趣的:(解析json字符串,生成json字符串,json字符串赋值)