在我使用Json库的时候先将json的头文件和实现代码加入到工程中去,头文件和实现代码可以这个地址下载:http://download.csdn.net/detail/yanchen0314/7497153
我写了一段简单的json的使用代码可供参考:
#include "json/autolink.h"
#include "json/config.h"
#include "json/features.h"
#include "json/forwards.h"
#include "json/json.h"
#include "json/json_batchallocator.h"
#include "json/reader.h"
#include "json/value.h"
#include "json/writer.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
Json::Value jVal;
jVal["json_int"] = 1;
jVal["json_cstring"] = "Hello";
std::cout<<"json int:"<<jVal["json_int"].asInt()<<" json string:"<<jVal["json_cstring"].asCString()<<endl;
/*-----------------------------------------------------------------------------------------------------------------*/
Json::Value jValArry;
jValArry["json_number"] = 10;
jValArry["json_node"] = "Node";
for(int i=0;i<10;i++)
{
Json::Value jValSun;
jValSun["json_int"] = i;
char pString[1024] = {0};
sprintf(pString,"This is Number:%d",i);
jValSun["json_cstring"] = (const char *)pString;
jValArry["json_arry"].append(jValSun);
}
std::string strJson = jValArry.toStyledString();
cout<<"json arry:"<<strJson<<endl;
/*------------------------------------------------------------------------------------------------------------------*/
if(jVal["json_int"].type() != Json::nullValue)
{
cout<<"jVal int:"<<jVal["json_int"].asInt()<<endl;
}
if(jVal["json_cstring"].type() != Json::nullValue)
{
cout<<"jVal string"<<jVal["json_cstring"].asString()<<endl;
}
/*------------------------------------------------------------------------------------------------------------------*/
Json::Value jArry;
Json::Reader reader;
reader.parse(strJson,jArry);
if(jArry["json_arry"].type() != Json::nullValue)
{
if(jArry["json_arry"].isArray())
{
int iSize = (int)jArry["json_arry"].size();
cout<<"json arry size:"<<iSize<<endl;
for(int j=0;j<iSize;j++)
{
if(jArry["json_arry"][j]["json_int"].type() != Json::nullValue)
{
int iValue = jArry["json_arry"][j]["json_int"].asInt();
cout<<"json arry int:"<<iValue<<endl;
}
if(jArry["json_arry"][j]["json_cstring"].type() != Json::nullValue)
{
std::string strValue = jArry["json_arry"][j]["json_cstring"].asCString();
cout<<"json arry string:"<<strValue<<endl;
}
}
}
}
return 0;
}
Json的使用:
1. 定义一个Json对象:Json::Value jVal;
2. 赋值int型:jVal[“json_int”] = 1; intiValue = jVal[“json_int”].asInt();
3. 赋值string类型:jVal[“json_cstring”] = “Hello”; std::stringstrValue = jVal[“json_cstring”].asCString();
std::string strValue = “Hello”; jVal[“json_string”] = strValue;
strValue = jVal[“json_string”].asString();
4. 使用json数组:
Json::Value jValArry;
for(int i=0;i<10;i++)
{
Json::Value jValSun;
jValSun[“json_int”] = i;
jValSun[“json_cstring”] = “Hello”;
jValArry[“json_arry”].append(jValSun);
}
5. 判断某个键值是否存在:
if(jVal[“json_int”].type()= = Json::nullValue)
{//没有该键值
do something!
}
else
{//有该键值
do something!
}
6. 解析json数组:
if(jValArry[“json_arry”].type()= = Json::nullValue)
{//没有该键值
do something!
}
else
{
if(jValeArry[“json_arry”].isArry())
{//判断该键值是否是数组
int iSize=(int) jValArry[“json_arry”].size();//得到数组元素的个数
for(int i=0;i<iSize;i++)
{
if(jValArry[“json_arry”][i][“json_int”].type()= = Json::nullValue)
{//数组中没有该元素
do something…..
}
else
{
int iType = jValArry[“json_arrry”][i][“json_int”].asInt();//得到数组中这个元素的值
}
if(jValArry[“json_arry”][i][“json_cstring”].type()= = Json::nullValue)
{//数组中没有该元素
do something!
}
else
{
if(jValArry[“json_arry”][i][“json_cstring”].type()= = Json::nullValue)
{
std::string strValue=jValArry[“json_arry”][i][“json_cstring”].asString();//得到数组中的元素
}
}
}
}
else
{
do something!
}
}
7. json对象序列化
std::stringstrJson = jValArry.toStyledString();
cout<<”jsonstring:”<<strJson<<endl;
8. json对象的反序列化
Json::Readerreader;
Json::ValuejVal;
Reader.parse(strJson,jVal);