JSON是一种轻量级的数据交换格式。

那么如何在Qt中处理json?首先要知道四个类。

第一个类QJsonDocument;

  把JSON格式的字符串转换成JSON对象/数组;或者把JSON对象/数组转换成JSON格式字符串。

第二个类QJsonObject;

  处理Json对象,{  }

第三个类QJsonArray;

  处理Json数组,[]

第四个类QjsonValue;

  可以是字符串、整型、布尔、浮点、json对象、json数组...

 

1、组织JSON数据写入磁盘

 

 1     QJsonObject obj;   //定义空对象
 2     QJsonObject sub;
 3     sub.insert("IP",QJsonValue("192.168.31.33"));//插入键值对
 4     sub.insert("Port",QJsonValue("9999"));
 5     obj.insert("server",QJsonValue(sub));
 6 
 7     QJsonDocument doc(obj);
 8     QByteArray data=doc.toJson();
 9     QFile file("tem1.json");
10     file.open(QIODevice::WriteOnly);
11     file.write(data);
12     file.close();

 

server:{

    IP:192.168.31.33,

    Port:9999

    }

2、把磁盘中JSON数据加载到内存

 

 

 1     QFile file("tem.json");
 2     file.open(QIODevice::ReadOnly);
 3     QByteArray data = file.readAll();
 4     file.close();
 5 
 6     QJsonDocument doc =QJsonDocument::fromJson(data);
 7     if(doc.isObject())
 8     {
 9         QJsonObject obj =doc .object();
10         QJsonValue  value=obj.value("server");
11         if(value.isObject())
12         {
13             QJsonObject sub=value.toObject();
14             QString IP = sub.value("IP").toString();
15             QString Port = sub.value("Port").toString();
16         }
17     }

   cJSON为我们提供一套操作json数据格式的API

  1、组织一个json数据格式的字符串写入磁盘

//创建一个json数组
cJSON *cJSON_CreateArray(void);
//创建一个json对象
cJSON *cJSON_CreateObject(void);
//向json数组种添加数据成员
void cJSON_AddItemToArray(cJSON *array, cJSON *item);
 //往json对象中添加数据成员
void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
//把其他类型转换成cjson*
cJSON *cJSON_CreateNumber(double num);
cJSON *cJSON_CreateString(const char *string);
 /* These utilities create an Array of count items. */
cJSON *cJSON_CreateIntArray(const int *numbers,int count);
cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
cJSON *cJSON_CreateStringArray(const char **strings,int count);

  一个小demo

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<unistd.h>
  5 #include<sys/stat.h>
  6 #include<sys/types.h>
  7 #include"cJSON.h"
  8 int main()
  9 {
 10     //创建一个json对象
 11     cJSON *obj = cJSON_CreateObject();
 12     //创建一个子对象
 13     cJSON *subobj= cJSON_CreateObject();
 14     //往子对象中添加键值
 15     cJSON_AddItemToObject(subobj,"type",cJSON_CreateString("章回体古装喜剧"));
 16     cJSON_AddItemToObject(subobj,"director",cJSON_CreateString("尚敬"));
 17     cJSON_AddItemToObject(subobj,"year",cJSON_CreateNumber(2006));
 18     cJSON *arry = cJSON_CreateArray();
 19     cJSON_AddItemToArray(arry,cJSON_CreateString("闫妮"));
 20     cJSON_AddItemToArray(arry,cJSON_CreateString("沙益"));
 21     cJSON_AddItemToArray(arry,cJSON_CreateString("姚晨"));
 22     cJSON_AddItemToArray(arry,cJSON_CreateString("喻恩泰"));
 23     cJSON_AddItemToArray(arry,cJSON_CreateNumber(123));
 24 
 25     cJSON *subsub = cJSON_CreateObject();
 26     cJSON_AddItemToObject(subsub,"天热了睡凉席",cJSON_CreateString("天冷了加件衣"));
 27     cJSON_AddItemToArray(arry,subsub);
 28     cJSON_AddItemToObject(subobj,"other",arry);
 29     //往对象中添加键值对
 30     cJSON_AddItemToObject(obj,"武林外传",subobj);
 31     //把json转换成字符串,方便写入磁盘
 32     char *data = cJSON_Print(obj);
 33     FILE *fd = fopen("cjson.cj","w");
 34     fwrite(data,1,strlen(data)+1,fd);
 35     fclose(fd);
 36     return 0;
 37 }
{
    "武林外传":    {
        "type":    "章回体古装喜剧",
        "director":    "尚敬",
        "year":    2006,
        "other":    ["闫妮", "沙益", "姚晨", "喻恩泰", 123, {
                "天热了睡凉席":    "天冷了加件衣"
            }]
}

   2、解析json,获取值

//cJSON结构体
 typedef struct cJSON {
    struct cJSON *next,*prev;   
     struct cJSON *child;     

     int type;                   /*类型*/
  
     char *valuestring;          /* The item's string, if type==cJSON_String */
     int valueint;               /* The item's number, if type==cJSON_Number */
     double valuedouble;         /* The item's number, if type==cJSON_Number */
 
     char *string;               /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
 } cJSON;

//解析一个json字符串为cJSON对象
cJOSN * cJSON_Parse(const char*buf);
//根据键查找值
cJSON * cJSON_GetObjectItem(cJSON*obj,const char *key);

  一个demo

void get_reg_info(char *reg_buf, char *user, char *pwd,char *email)
 {
 
     /*json数据如下
     {
         userName:xxxx,
         firstPwd:xxx,
         email:xxx
     }
     */
 
//解析json包
//解析一个json字符串为cJSON对象
     cJSON * root = cJSON_Parse(reg_buf);
     if(NULL == root)
     {
         //出错处理
     }
    
     //返回指定字符串对应的json对象
     //用户
     cJSON *child1 = cJSON_GetObjectItem(root, "userName");
     if(NULL == child1)
     {
           //出错处理    
     }
    
     strcpy(user, child1->valuestring);

     cJSON *child2=cJSON_GetObjectItem(root,"fristPwd");
     if(NULL ==child2)
     {}
      strcpy(pwd,child2->valuestring);

    cJSON *child2 = cJSON_GetObjectItem(root,"email");
    strcpy(email,child2->valuestring);
   cJSON_Delete(root); }