在VC中生成嵌套JSON

#include "jsoncpp/include/json.h"

 //用网上下载的JSON类

 

 

 Json::Value root;
 Json::Value var;
 Json::Value jFdata;

 

//比方说,一个目录级别如下:

 c://files//testFile//datas//{1.text,2.text,3.text .4.text}

 

 //生成的目录如下:

{"folders":"files","data":[{"folders":"testFile","data":[{"folders":"datas","data":[{"filename":"1.text"},{"filename":"2.text"},{"filename":"3.text"},{"filename":"4.text"}]}]}]}

 

 

 CStringArry folderslist;//假设已经保存了所有的目录结构

 folderslist.add("files");

 folderslist.add("ftestFile");

 folderslist.add("datas");

 

 

 CStringArry fileslist;//假设已经保存了所有的目录结构

 fileslist.add("1.text");

 fileslist.add("2.text");

 fileslist.add("3.text");

 fileslist.add("4.text");

 

 //先生成四个文件JSON

   Json::Value jlData;

   for(int ifilePos = 0;ifilePos<4,ifilePos++)

{

   Json::Value piece_ex;//here it store just one piece
   //next 4 lines to apply piece value to json struct
   piece_ex["folders"]       =  fileslist.get(ifilePos );
     jlData.append(piece_ex);//ok,yes we just have apply One piece ,then push back to the array

}

 

//生成嵌套JSON目录,从最深层往外生成

Json::Value jcFdata;
 for(int iFoldersPos = 2 ;iFoldersPos >=0 ;--iFoldersPos)
 {
  Json::Value jccData;
  Json::Value jccRoot;
  jccData["fname"]   = folderslist.get(iFoldersPos );
   if(iFoldersPos == listFolders.GetCount()-1)
  {
   jccData["folders"] = jlData;
  }
  else
  {
   jccData["folders"] = jcFdata;
  }
  jcFdata = jccData;
 }
 jFdata.append(jcFdata);

 

 var["data"] = jFdata;//yes,store pieces in var [Value]
 root.append(var);
 Json::FastWriter writer;
 CString szData(writer.write(var).c_str());

VC6.0 解析来自 Web 的 JSON 数据,你可以使用第三方库来实现。由于 VC6.0 是一个较旧的版本,它可能不支持现代的 JSON 解析器。以下是一种可能的解决方案: 1. 首先,你需要下载并集成一个适用于 VC6.0 的 JSON 解析库。例如,你可以尝试使用 cJSON(https://github.com/DaveGamble/cJSON)或 JSON for Modern C++(https://github.com/nlohmann/json)。 2. 下载所选库的源代码,并将其添加到你的 VC6.0 项目。确保将头文件和源文件正确地添加到项目。 3. 在你的代码,包含所选库的头文件,并使用其提供的 API 进行 JSON 解析。具体的步骤和函数将取决于你选择的库。 下面是使用 cJSON 库解析 JSON 数据的一个简单示例: ```c #include <stdio.h> #include <stdlib.h> #include "cJSON.h" int main() { const char* json_string = "{\"name\": \"John\", \"age\": 30}"; cJSON* root = cJSON_Parse(json_string); if (root == NULL) { printf("Error parsing JSON!\n"); return 1; } cJSON* name = cJSON_GetObjectItem(root, "name"); if (name == NULL || !cJSON_IsString(name)) { printf("Error retrieving 'name' from JSON!\n"); cJSON_Delete(root); return 1; } printf("Name: %s\n", name->valuestring); // Clean up cJSON_Delete(root); return 0; } ``` 注意,以上代码仅为示例,具体的解析步骤和函数可能因所选库而异。你需要根据你的实际需求和所选库的文档进行相应的修改。 希望这能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值