背景需求:
需要批量处理格式相同,内容不同的Json格式的数据,每个单独的数据如下存储:
{
"hash":"fasdfsafdfdfdf",
"keyword":"刘德华-冰雨0",
"duration":10001
}
待处理的数据为多个上述格式的数据
方法1:Json::value对象数组
Json格式的输入和输出。输入为Json::value数组,在函数内部将其解析为一个个的Json对象,每个Json::value的对象如下:
由一个数组对多个类型的对象进行存储。
在函数内部,对该数组进行解析,分别存放于hash[500],keyword[500]和duration[500]各个数组之中,在数组中相同下标的对应同一组数据,即同一个Json::value对象中的关键字和值。
在函数内部进行一定处理之后,再以Json::value的格式进行输出返回。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <string>
#include <iostream>
#include<json/json.h>
using namespace std;
#define POSTURL "http://www.xiami.com/member/login"
#define POSTFIELDS "email=myemail@163.com&password=mypassword&autologin=1&submit=登 录&type="
#define FILENAME "curlposttest.log"
void Getjson(Json::Value *a, Json::Value *b, std::string *json_my)
{
Json::Reader reader;
Json::StyledWriter styled_writer1;//另一种格式化的方式,输出结果的格式不一样
for(int i=0; i<3; i++)
{
if (!reader.parse(json_my[i], b[i]))
{};//return 0;
cout << styled_writer1.write(b[i]) <<endl;
}
}
int main(int argc, char *argv[])
{
std::string json_my[10];
json_my[0]="{\"hash\":\"fasdfsafdfdfdf\",\"keyword\": \"刘德华-冰雨\",\"duration\":\"16080455\"}";
//const char *json_my[50] = "{\"hash\":\"fasdfsafdfdfdf\",\"keyword\": \"刘德华-冰雨\",\"duration\":\"16080455\"}";
json_my[1]="{\"hash\":\"fasdfsafdfdfdf\",\"keyword\": \"刘德华-1\",\"duration\":\"16080455\"}";
//int num=json_my[;
Json::Reader reader;
Json::Value json_object[50];//=new Json::Value;
Json::Value json_a[50];//=new Json::Value;
Json::StyledWriter styled_writer1;//另一种格式化的方式,输出结果的格式不一样
Getjson(json_a, json_object,json_my);
return 0;
}
方法2:字符串进行存储json格式数据,注意在采用字符串存储jason数据的时候,对于引号,需要用斜杠进行标示。
下面的代码实现的功能是std::string json_new存储json数据,用Json::Reader对该字符串进行解析,并进行打印,从打印结果可以更为清晰认知json格式的排版、同时将解析之后的数据存储在数组中,方便后续的进一步处理。
代码:
std::string json_my[50];
//第二种表示Json字符串的方式
std::string json_new="[{\"hash\":\"fasdfsafdfdfdf\",\"keyword\":\"刘德华-冰雨0\",\"duration\":10001},\
{\"hash\":\"fasdfsafdfdfdf\",\"keyword\":\"刘德华-冰雨1\",\"duration\":10002},\
{\"hash\":\"fasdfsafdfdfdf\",\"keyword\":\"刘德华-冰雨2\",\"duration\":10002}]";
Json::Reader readerNew;
Json::Value jsonNew;
Json::StyledWriter writerNew;
if(readerNew.parse(json_new,jsonNew))
cout << writerNew.write(jsonNew) <<endl;<span style="display: none; width: 0px; height: 0px;" id="transmark"></span>
int jsonsize = jsonNew.size();//统计该数组中的{}对数,即统计待处理的任务数量
//进行分离,以数组形式进行存储
std::string *hashNew=new std::string [jsonsize];
std::string *keywordNew=new std::string [jsonsize];
int *durationNew=new int [jsonsize];
for(int i=0; i<jsonsize; i++)
{
hashNew[i]=jsonNew[i]["hash"].asString();
//cout<<hashNew[i]<<endl;
keywordNew[i]=jsonNew[i]["keyword"].asString();
//cout<<keywordNew[i]<<endl;
durationNew[i]=jsonNew[i]["duration"].asInt();
//cout<<durationNew[i]<<endl;
}
对于过长的字符串,在换行的时候注意加一个"\",进行代码的连接。
打印结果如下:
读取部分的补充,将数据存放于json文档中,通过对该文档的读取实现待处理任务的读取,
代码如下:
//通过文件形式进行读取
cout<<"通过读取json文件格式:"<<endl;
std::string stringTemp;
std::string stringFile;
Json::Value jsonFile;
std::ifstream filenew( "//home//liujiepeng//liutest//9-2.json" ); // declare file stream:
//std::ifstream filenew( "//home//liujiepeng//liutest//9-2.txt" ); // declare file stream:
if(filenew.is_open())
{
while(filenew>>stringTemp)
stringFile += stringTemp;
}
Json::Reader readerFile;
Json::StyledWriter writerFile;//另一种格式化的方式,输出结果的格式不一样
if(readerFile.parse(stringFile,jsonFile))
{
cout << writerFile.write(jsonFile) <<endl;
}
int jsonFileSize = jsonFile.size();
cout<<"该json文件中有"<<jsonFileSize<<"个待处理的任务"<<endl;
9-2.json文件的内容如下所示:
运行结果如下:
备注:
此外,另一种的代码连接方法是采用引号,如下图所示:
参考资料:
http://www.cnblogs.com/westfly/archive/2011/06/13/json_start.html
http://blog.csdn.net/hzyong_c/article/details/7163589
http://www.cppblog.com/wanghaiguang/archive/2015/05/27/205020.html
http://www.json.cn/
http://www.open-open.com/lib/view/open1394002733615.html
http://blog.csdn.net/songshiMVP1/article/details/48707041?ref=myread