json cpp 的简单操作
2017-08-07 13:42
274 查看
1、将json字符串转换成json对象
1)第一种:利用 stringstream 类
json::Value jsonobj
char* str = new char[]
memcopy()
std::stringstream iss(str)
iss >> jsonobj
2)第二种
Json::Reader jsonReader;
jsonReader.parse(char* buf/*in*/, Json::Value json_value/*out*/)
2、将json对象转换成json字符串
Json::Value jsonValue;
Json::FastWriter fastWriter;
anchorListStr = fastWriter.write(jsonValue);
3、JsonCpp 的简单使用
以下为示例代码:
JsonCpp 是一个C++用来处理JSON 数据的开发包。下面讲一下怎么使用JsonCpp来序列化和反序列化Json对象,以实际代码为例子。
1)反序列化Json对象
比如一个Json对象的字符串序列如下,其中”array”:[...]表示Json对象中的数组:
{“key1″:”value1″,”array”:[{"key2":"value2"},{"key2":"value3"},{"key2":"value4"}]},那怎么分别取到key1和key2的值呢,代码如下所示:
std::string strValue = “{\”key1\”:\”value1\”,\”array\”:[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}”;
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value)){
std::string out = value["key1"].asString();
std::cout << out << std::endl;
const Json::Value arrayObj = value["array"];
for (int i=0; i<arrayObj.size(); i++){
out = arrayObj[i]["key2"].asString();
std::cout << out;
if (i != arrayObj.size() – 1 )
std::cout << std::endl;
}
}
2)序列化Json对象
先构建一个Json对象,此Json对象中含有数组,然后把Json对象序列化成字符串,代码如下:
Json::Value root;
Json::Value arrayObj;
Json::Value item;
for (int i=0; i<10; i++)
{
item["key"] = i;
arrayObj.append(item);
}
root["key1"] = “value1″;
root["key2"] = “value2″;
root["array"] = arrayObj;
root.toStyledString();
std::string out = root.toStyledString();
std::cout << out << std::endl;
3)删除Json对象
std::string strContent = "{\"key\":\"1\",\"name\":\"test\"}";
Json::Reader reader;
Json::Value value;
if (reader.parse(strContent, value)){
Json::Value root=value;
root.removeMember("key");
printf("%s \n",root.toStyledString().c_str());
}
1)第一种:利用 stringstream 类
json::Value jsonobj
char* str = new char[]
memcopy()
std::stringstream iss(str)
iss >> jsonobj
2)第二种
Json::Reader jsonReader;
jsonReader.parse(char* buf/*in*/, Json::Value json_value/*out*/)
2、将json对象转换成json字符串
Json::Value jsonValue;
Json::FastWriter fastWriter;
anchorListStr = fastWriter.write(jsonValue);
3、JsonCpp 的简单使用
以下为示例代码:
JsonCpp 是一个C++用来处理JSON 数据的开发包。下面讲一下怎么使用JsonCpp来序列化和反序列化Json对象,以实际代码为例子。
1)反序列化Json对象
比如一个Json对象的字符串序列如下,其中”array”:[...]表示Json对象中的数组:
{“key1″:”value1″,”array”:[{"key2":"value2"},{"key2":"value3"},{"key2":"value4"}]},那怎么分别取到key1和key2的值呢,代码如下所示:
std::string strValue = “{\”key1\”:\”value1\”,\”array\”:[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}”;
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value)){
std::string out = value["key1"].asString();
std::cout << out << std::endl;
const Json::Value arrayObj = value["array"];
for (int i=0; i<arrayObj.size(); i++){
out = arrayObj[i]["key2"].asString();
std::cout << out;
if (i != arrayObj.size() – 1 )
std::cout << std::endl;
}
}
2)序列化Json对象
先构建一个Json对象,此Json对象中含有数组,然后把Json对象序列化成字符串,代码如下:
Json::Value root;
Json::Value arrayObj;
Json::Value item;
for (int i=0; i<10; i++)
{
item["key"] = i;
arrayObj.append(item);
}
root["key1"] = “value1″;
root["key2"] = “value2″;
root["array"] = arrayObj;
root.toStyledString();
std::string out = root.toStyledString();
std::cout << out << std::endl;
3)删除Json对象
std::string strContent = "{\"key\":\"1\",\"name\":\"test\"}";
Json::Reader reader;
Json::Value value;
if (reader.parse(strContent, value)){
Json::Value root=value;
root.removeMember("key");
printf("%s \n",root.toStyledString().c_str());
}
相关文章推荐
- str()和repr()(及''操作符)以及python对json的简单操作
- JS操作json简单示例
- Jsoncpp的简单使用
- java操作json的简单使用
- jsoncpp简单示例
- java操作json字符串 与 js操作json字符串简单例子
- 在Javascript操作JSON对象,增加 删除 修改的简单实现
- Jsoncpp在linux中的安装、编译及其简单应用
- jsoncpp文件操作
- jsoncpp文件操作
- jsoncpp 简单示例
- c#3.5关于JSON简单操作的实用帮助类(泛型实现)
- c++ jsoncpp操作
- jquery 对 cookie存储 json格式的简单操作
- jquery 对 cookie存储 json格式的简单操作
- jsoncpp简单示例
- c#3.5关于JSON简单操作的实用帮助类(泛型实现)
- C++简单使用Jsoncpp来读取写入json文件
- jsoncpp简单示例
- 6、JsonCpp简单使用(2)