#include <iostream>
#include <string>
#include <fstream>
#include "cJSON.h"
using namespace std;
//从文件读json数据到string
void Read_cJSON(const char *path, string &data) {
ifstream fr(path);
if (!fr) {
cout << "can`t open file!" << endl;
return;
}
char ch;
while ((ch = fr.get()) != EOF) {
data.push_back(ch);
}
fr.close();
}
//解析json字符串
void AnalyzeJson(cJSON *root) {
for (int j = 0; j < cJSON_GetArraySize(root); j++) {
cJSON* item = cJSON_GetArrayItem(root, j);
if (item->type == cJSON_Object) {//对象
if (!cJSON_IsNull(item)) {
if (item->string)//可能存在对象名为NULL{"key":"value"}对象名为NULL )
cout << "cJSON_Object "<< j << " :"<< item->string << " cJSON_GetArraySize(Object): " << cJSON_GetArraySize(item) << endl;
AnalyzeJson(item);
}
}
else if (item->type == cJSON_Array) {
if (!cJSON_IsNull(item)) {//数组
if(item->string)
cout << "cJSON_Array " << j << " :" << item->string << " cJSON_GetArraySize(Array): " << cJSON_GetArraySize(item) << endl;
AnalyzeJson(item);
}
}
else {
if (!cJSON_IsNull(item)) {//键值对,注意键类型
if(item->type == cJSON_String)
cout <<"key(string) " << j << " : " << item->string << "; value:"<< item->valuestring<<endl;
if (item->type == cJSON_Number)
cout << "key(number) " << j << " : " << item->string << "; value:" << item->valueint << endl;
}
}
}
cout << endl << endl;
}
int main()
{
string data;
Read_cJSON("./json.json", data);
cJSON *root = cJSON_Parse(data.c_str());
cout << "all ObjectSize(): " << cJSON_GetArraySize(root) << endl;
AnalyzeJson(root);
cJSON_Delete(root);
}
/*
///测试数据
{
"cJSON_Object1": {
"cJSON_Object1-1": {
"key1": "valuestring",
"key2": 0,
"key3": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"key4": "0.000000",
"key5": "0.000000",
"key6": "0.000000",
"key7": "0.000000",
"key8": "18.542000",
"key9": "23.088600",
"key10": "27.094180",
"key11": "27.094180",
"key12": 0,
"key13": 24
},
"cJSON_Object1-2": {
"key1": "valuestring",
"key2": 0,
"key3": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"key4": "0.000000",
"key5": "0.000000",
"key6": "0.000000",
"key7": "0.000000",
"key8": "18.542000",
"key9": "23.088600",
"key10": "27.094180",
"key11": "27.094180",
"key12": 0,
"key13": 24
},
"key14": 1,
"key15": 2,
"key16": 3
},
"cJSON_Object2": [
{
"Shape_num": 1,
"Shape_List0": {
"iShapeType": 24,
"iIsPositive": 0,
"dcx": 0,
"dcy": 0,
"diameter": 0,
"dx1": 71.075042,
"dy1": 66.265044,
"dx2": 71.399908,
"dy2": 65.940178,
"dwidth": 0.1016,
"TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"Polygon_DARCP_num": 0,
"Polygon_DARCP_List": {
}
}
},
{
"Shape_num": 1,
"Shape_List1": {
"iShapeType": 24,
"iIsPositive": 0,
"dcx": 0,
"dcy": 0,
"diameter": 0,
"dx1": 71.075042,
"dy1": 66.265044,
"dx2": 71.399908,
"dy2": 65.940178,
"dwidth": 0.1016,
"TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"Polygon_DARCP_num": 0,
"Polygon_DARCP_List": {
}
}
},
{
"Shape_num": 2,
"Shape_List1": {
"iShapeType": 24,
"iIsPositive": 0,
"dcx": 0,
"dcy": 0,
"diameter": 0,
"dx1": 71.075042,
"dy1": 66.265044,
"dx2": 71.399908,
"dy2": 65.940178,
"dwidth": 0.1016,
"TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"Polygon_DARCP_num": 0,
"Polygon_DARCP_List": {
}
},
"Shape_List2": {
"iShapeType": 24,
"iIsPositive": 0,
"dcx": 0,
"dcy": 0,
"diameter": 0,
"dx1": 71.075042,
"dy1": 66.265044,
"dx2": 71.399908,
"dy2": 65.940178,
"dwidth": 0.1016,
"TVYArcPoint": "{0.000000,0.000000,0.000000,0.000000,0.000000}",
"Polygon_DARCP_num": 0,
"Polygon_DARCP_List": {
}
}
}
]
}
*/
整个json包含两个元素 all ObjectSize(): 2
cJSON_Object1(对象) cJSON_Array(数组)
1、cJSON_Object1对象包含五个元素 cJSON_GetArraySize(Object1): 5:
两个对象: cJSON_Object1-1 和 cJSON_Object1-2; 三个键值对: key14、15、16
1-1、cJSON_Object1-1 包含13个键值对(string类型和int型)
2、cJSON_Array数组包含三个元素, cJSON_GetArraySize(Array): 3
三个对象,对象名均为NULL ( Obj1{"key":"value"}对象名为Obj1 ; {"key":"value"}对象名为NULL )
2-1、每个对象包含一个键值对Shape_num和一个对象Shape_List1,第三个对象包含一个键值对和两个对象Shape_List1和Shape_List2
2-2、每个Shape_List对象包含13个键值对 cJSON_GetArraySize(Object): 13
///输出结果
all ObjectSize(): 2 //整个json包含两个元素 cJSON_Object1(对象) cJSON_Array(数组)
cJSON_Object 0 :cJSON_Object1 cJSON_GetArraySize(Object): 5 //两个对象: cJSON_Object1-1 和 cJSON_Object1-2; 三个键值对: key14、15、16
cJSON_Object 0 :cJSON_Object1-1 cJSON_GetArraySize(Object): 13
key(string) 0 : key1; value:valuestring
key(number) 1 : key2; value:0
key(string) 2 : key3; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(string) 3 : key4; value:0.000000
key(string) 4 : key5; value:0.000000
key(string) 5 : key6; value:0.000000
key(string) 6 : key7; value:0.000000
key(string) 7 : key8; value:18.542000
key(string) 8 : key9; value:23.088600
key(string) 9 : key10; value:27.094180
key(string) 10 : key11; value:27.094180
key(number) 11 : key12; value:0
key(number) 12 : key13; value:24
cJSON_Object 1 :cJSON_Object1-2 cJSON_GetArraySize(Object): 13
key(string) 0 : key1; value:valuestring
key(number) 1 : key2; value:0
key(string) 2 : key3; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(string) 3 : key4; value:0.000000
key(string) 4 : key5; value:0.000000
key(string) 5 : key6; value:0.000000
key(string) 6 : key7; value:0.000000
key(string) 7 : key8; value:18.542000
key(string) 8 : key9; value:23.088600
key(string) 9 : key10; value:27.094180
key(string) 10 : key11; value:27.094180
key(number) 11 : key12; value:0
key(number) 12 : key13; value:24
key(number) 2 : key14; value:1
key(number) 3 : key15; value:2
key(number) 4 : key16; value:3
cJSON_Array 1 :cJSON_Object2 cJSON_GetArraySize(Array): 3 //三个对象
//对象1
key(number) 0 : Shape_num; value:1
cJSON_Object 1 :Shape_List0 cJSON_GetArraySize(Object): 13
key(number) 0 : iShapeType; value:24
key(number) 1 : iIsPositive; value:0
key(number) 2 : dcx; value:0
key(number) 3 : dcy; value:0
key(number) 4 : diameter; value:0
key(number) 5 : dx1; value:71
key(number) 6 : dy1; value:66
key(number) 7 : dx2; value:71
key(number) 8 : dy2; value:65
key(number) 9 : dwidth; value:0
key(string) 10 : TVYArcPoint; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(number) 11 : Polygon_DARCP_num; value:0
cJSON_Object 12 :Polygon_DARCP_List cJSON_GetArraySize(Object): 0
//对象2
key(number) 0 : Shape_num; value:1
cJSON_Object 1 :Shape_List1 cJSON_GetArraySize(Object): 13
key(number) 0 : iShapeType; value:24
key(number) 1 : iIsPositive; value:0
key(number) 2 : dcx; value:0
key(number) 3 : dcy; value:0
key(number) 4 : diameter; value:0
key(number) 5 : dx1; value:71
key(number) 6 : dy1; value:66
key(number) 7 : dx2; value:71
key(number) 8 : dy2; value:65
key(number) 9 : dwidth; value:0
key(string) 10 : TVYArcPoint; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(number) 11 : Polygon_DARCP_num; value:0
cJSON_Object 12 :Polygon_DARCP_List cJSON_GetArraySize(Object): 0
//对象3
key(number) 0 : Shape_num; value:2
cJSON_Object 1 :Shape_List1 cJSON_GetArraySize(Object): 13
key(number) 0 : iShapeType; value:24
key(number) 1 : iIsPositive; value:0
key(number) 2 : dcx; value:0
key(number) 3 : dcy; value:0
key(number) 4 : diameter; value:0
key(number) 5 : dx1; value:71
key(number) 6 : dy1; value:66
key(number) 7 : dx2; value:71
key(number) 8 : dy2; value:65
key(number) 9 : dwidth; value:0
key(string) 10 : TVYArcPoint; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(number) 11 : Polygon_DARCP_num; value:0
cJSON_Object 12 :Polygon_DARCP_List cJSON_GetArraySize(Object): 0
cJSON_Object 2 :Shape_List2 cJSON_GetArraySize(Object): 13
key(number) 0 : iShapeType; value:24
key(number) 1 : iIsPositive; value:0
key(number) 2 : dcx; value:0
key(number) 3 : dcy; value:0
key(number) 4 : diameter; value:0
key(number) 5 : dx1; value:71
key(number) 6 : dy1; value:66
key(number) 7 : dx2; value:71
key(number) 8 : dy2; value:65
key(number) 9 : dwidth; value:0
key(string) 10 : TVYArcPoint; value:{0.000000,0.000000,0.000000,0.000000,0.000000}
key(number) 11 : Polygon_DARCP_num; value:0
cJSON_Object 12 :Polygon_DARCP_List cJSON_GetArraySize(Object): 0
简单的json读取方式
//简单的单层结构json读取方式
void AnalyzeJson2(string data) {
cJSON *root = cJSON_Parse(data.c_str());
for (int i = 0; i < cJSON_GetArraySize(root); i++) {
cJSON *val = cJSON_GetArrayItem(root, i);
cout << val->string << " " << val->valuestring << endl;
cout << cJSON_GetArrayItem(root, i)->string << " " << cJSON_GetArrayItem(root, i)->valuestring << endl;
}
cJSON* item = cJSON_GetArrayItem(root, 0);
while (item) {
//按照key匹配value,cJSON_GetObjectItem在匹配时不区分大小写,默认获取第一个匹配到的key
cJSON *val = cJSON_GetObjectItem(root, "name");
if(val->string)
cout << val->string << " " << val->valuestring << endl;
//cJSON_GetObjectItemCaseSensitive在匹配时区分大小写
cJSON *valcaseS = cJSON_GetObjectItemCaseSensitive(root, "Name");
if(valcaseS->string)
cout << valcaseS->string << " " << valcaseS->valuestring << endl;
cout << item->string << " " << item->valuestring << endl << endl;
item = item->next;
}
cout << cJSON_GetObjectItem(root, "name")->valuestring << endl;
cout << cJSON_GetObjectItem(root, "Name")->valuestring << endl;
cout << cJSON_GetObjectItem(root, "NAME")->valuestring << endl;
cJSON_Delete(root);
}
/*
测试用例
{
"title": "group",
"Name": "zhangsan",
"name": "lisi",
"NaMe": "wangwu",
"NAME": "zhaoliu",
"sex": "girl",
"Age": "three"
}
*/
顺便附上jsoncpp的简单格式未知key匹配方法
//读取json
void AnalyzeJsonCpp(string data)
{
Json::Reader reader1;
Json::Value root1;
reader1.parse(data, root1);
//获取所有json中的key到string数组里面
Json::Value::Members members;//members-->vector<string>
members = root1.getMemberNames();//获取所有key
for (auto key : members) {
cout << key <<" : "<< root1[key].asString() <<";"<< endl;
}
auto size = members.size();
}