1、前言
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似,而Jsoncpp是一个跨平台开源库,其代码下载为
https://sourceforge.net/projects/jsoncpp/
2、海思平台编译
交叉编译工具为aarch64-himix100-linux-g++
3、使用
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <json/json.h>
void TEST_READ()
{
const char* str = "{\"name\": \"Jsoncpp\",\"version\": \"v1.0\",\"date\": \"2019-05-23 15:50:59\",\"id\": 100}";
Json::Reader reader;
Json::Value root;
std::string sname;
std::string sversion;
std::string sdate;
int iid;
if (reader.parse(str, root))
{
if(root.isMember("name"))
{
sname = root["name"].asString();
}
if(root.isMember("version"))
{
sversion = root["version"].asString();
}
if(root.isMember("date"))
{
sdate = root["date"].asString();
}
if(root.isMember("id"))
{
iid = root["id"].asInt();
}
std::string strroot = root.toStyledString();
printf("[%s:%d]-root=%s name=%s version=%s date=%s id=%d\n", __FUNCTION__,__LINE__,strroot.c_str(),sname.c_str(),sversion.c_str(),sdate.c_str(),iid);
}
}
void TEST_WRTIE()
{
Json::Value root;
root["PARAM"]["name"] = "jsoncpp";
root["PARAM"]["version"] = "v1.0";
root["PARAM"]["date"] = "2019-05-23 15:50:59";
Json::FastWriter writer;
std::string result = writer.write(root);
if( NULL == result.c_str() )
{
printf("alarm json is null!\n");
return;
}
else
{
printf("[%s:%d]-json=%s\n", __FUNCTION__,__LINE__,result.c_str());
}
}
int main()
{
TEST_READ();
TEST_WRTIE();
return 0;
}