cJSON数组读取方式

 第一种

cJSON *root = cJSON_Parse(buff); 

cJSON *_Section = cJSON_GetObjectItem(root,“Section”);

for (int i = 0; i < cJSON_GetArraySize(_Section); i++)
{
	cJSON *svalue = cJSON_GetArrayItem(_Section, i);
	cout << cJSON_GetObjectItem(svalue, "test1")->valuestring << endl;
	cout << cJSON_GetObjectItem(svalue, "test2")->valuestring << endl;
    cJSON *ssection = cJSON_GetObjectItem(svalue, "STest");
    for(int j = 0; j < cJSON_GetArraySize(ssection); j++)
    {
        cJSON *ssvalue = cJSON_GetArrayItem(ssection, j);
        cout << cJSON_GetObjectItem(ssvalue, "sstest1")->valuestring << endl;
    }
}

//当cJSON_GetArraySize(_Section)超出一定大小时速度会变得极慢(约60000),时间复杂度以指数增长(30000左右察觉不来)

 第二种

typedef struct Symbol {
	int One;
	int Two;
	struct Three {
		string Sone;
		string Stwo;
	}Drawing;
	double Four;
	double Five;
	int Six;
}Symbol;

cJSON *_Section = cJSON_GetObjectItem(root, "Section");
	vector
vec_Sec; Section sub_section; int Section_size = cJSON_GetArraySize(_Section);//可以获取长度 if (_Section) { cJSON *skey4 = _Section->child; while (skey4) { sub_section.One = atoi(cJSON_GetObjectItem(skey4, "One")->valuestring); sub_section.Two = atoi(cJSON_GetObjectItem(skey4, "Two")->valuestring); cJSON *_Three = cJSON_GetObjectItem(skey4, "Three"); if (_Three) { cJSON *sskey = _Three->child; while (sskey) { sub_section.Drawing.Sone = cJSON_GetObjectItem(sskey, "Sone")->valuestring; sub_section.Drawing.Stwo = cJSON_GetObjectItem(sskey, "Stwo")->valuestring; sskey = sskey->next; } } sub_section.Four = atof(cJSON_GetObjectItem(skey4, "Four")->valuestring); sub_section.Five = atof(cJSON_GetObjectItem(skey4, "Five")->valuestring); sub_section.Six = atoi(cJSON_GetObjectItem(skey4, "Six")->valuestring); vec_Sec.push_back(sub_section); skey4 = skey4->next; } } /* //格式 "Section":[ { "One":"1", "Two":"0", "Three":[ { "Sone":"Fill", "Stwo":"R,-2.5400,-2.2073,2.5400,2.2073" }], "Four":"5.0800", "Five":"4.4145", "Six":"2" }, { "One":"2", "Two":"0", "Three":[ { "Sone":"Fill", "Stwo":"A,1,0.4952,0.4445,0.4444,0.4953,0.4444,0.4445;P,1,-0.4446,0.4953;A,1,-0.4446,0.4953,-0.4954,0.4445,-0.4446,0.4445;P,1,-0.4954,-0.4445;A,1,-0.4954,-0.4445,-0.4547,-0.4928,-0.4453,-0.4436;P,1,-0.4446,-0.4953;P,1,0.4444,-0.4953;A,1,0.4444,-0.4953,0.4952,-0.4445,0.4444,-0.4445;A,1,0.4952,-0.4445,0.4952,-0.4369,0.4441,-0.4407;P,1,0.4952,0.4445" }], "Four":"0.9907", "Five":"0.9906", "Six":"4" } ] */

 

你可能感兴趣的:(cJSON数组读取方式)