cJSON数组读取方式

本文探讨了使用cJSON库解析大规模JSON数据时的两种不同方法,并对比了它们的性能表现。第一种方法直接利用cJSON函数遍历JSON数组,但当数据量达到一定规模时,效率显著下降。第二种方法通过定义结构体并使用vector存储解析结果,有效提高了处理大量数据时的速度。文章深入分析了两种方法的时间复杂度和实际应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 第一种

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<Section> 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"
    }
  ]
*/




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值