#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
// #include <unistd.h>
#include "cJSON.h"
/* exact-width signed integer types */
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
/* exact-width unsigned integer types */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef struct
{
char EventName[32];
uint8_t MathType;
_Bool MathValue;
} __Event;
typedef struct
{
uint16_t RegAddr;
uint8_t RegType;
uint8_t WriteType;
} __Param;
typedef struct
{
char Name[32];
uint8_t SignalType;
int32_t UpRangeValue;
int32_t DownRangeValue;
uint16_t DataType;
uint16_t Decimal;
char Unit[8];
__Param Param;
__Event Event;
} __VarList;
typedef struct
{
__VarList VarList;
char DevName[32];
} __VARConfigs;
typedef struct
{
int id;
char firstName[32];
char lastName[32];
char email[64];
int age;
float height;
} people;
typedef struct Data
{
uint32_t u32Variable;
char Value[128];
uint32_t u32Min;
uint32_t u32Max;
} T_Data, *PT_Data;
typedef struct PAGEConfigs
{
uint32_t u32Id;
uint16_t u16X;
uint16_t u16Y;
uint16_t u16W;
uint16_t u16H;
T_Data date;
} T_PAGEConfigs, *PT_PAGEConfigs;
char *ppl1 = "{\
\"person\" :\
{\
\"firstName\" : \"z\",\
\"lastName\" : \"jadena\",\
\"email\" : \"jadena@126.com\",\
\"age\" : 8,\
\"height\" : 1.17\
}\
}";
char *ppl =
"{ \
\"people\":[ \
{\"firstName\":\"z\",\"lastName\":\"Jason\",\"email\":\"bbbb@126.com\",\"height\":1.67}, \
{\"lastName\":\"jadena\",\"email\":\"jadena@126.com\",\"age\":8,\"height\":1.17}, \
{\"email\":\"cccc@126.com\",\"firstName\":\"z\",\"lastName\":\"Juliet\",\"age\":36,\"height\":1.55} \
] \
}";
char *strArr =
"{ \
\"VARConfigs\": [ \
{ \
\"VarList\": [ \
{ \
\"Name\": \"var\", \
\"DataType\": 1, \
\"Decimal\": 0, \
\"Unit\": \"int\", \
\"SignalType\": 1, \
\"UpRangeValue\": 0.0, \
\"DownRangeValue\": 50000.0, \
\"Param\": { \
\"RegAddr\": 4660, \
\"RegType\": 1, \
\"WriteType\": 1 \
}, \
\"Event\": { \
\"EventName\": \"\", \
\"MathType\": 6, \
\"MathValue\": true \
} \
} \
], \
\"PortName\": \"11\", \
\"DevName\": \"11\" \
} \
] \
}";
char *message =
"{ \
\"name\":\"mculover666\", \
\"age\": 22, \
\"weight\": 55.5, \
\"address\": \
{ \
\"country\": \"China\",\
\"zip-code\": 111111\
}, \
\"skill\": [\"c\", \"Java\", \"Python\"],\
\"student\": false \
}";
char *GUIstrArr = "{\"PAGEConfigs\":[{\"id\":123,\"x\":1066,\"y\":606,\"w\":86,\"h\":86,\"data\":{\"variable\":123,\"value\":\"仪表盘\",\"min\":0,\"max\":100}}]}";
void dofile(char *filename); /* Read a file, parse, render back, etc. */
int cJSON_to_struct_array(char *text, people worker[]);
int cJSON_to_struct(char *json_string, people *person);
int my_test(void);
int _cJSON_to_struct_array(char *text, __VARConfigs config[]);
int GUI_cJSON_to_struct_array(char *text, T_PAGEConfigs config[]);
int main(int argc, char **argv)
{
// setbuf(stdout, 0);
// dofile("json_str1.txt");
// dofile("json_str2.txt");
// dofile("json_str3.txt");
printf("/********************************worker[3]********************************/\r\n");
people worker[3];
cJSON_to_struct_array(ppl, worker);
printf("\r\n");
// printf("/********************************people********************************/\r\n");
// people worker1;
// cJSON_to_struct(ppl1, &worker1);
// printf("\r\n");
// printf("/*********************************my_test*******************************/\r\n");
// my_test();
// printf("/*********************************VARConfigs[x]*******************************/\r\n");
// __VARConfigs VARConfigs1[1];
// _cJSON_to_struct_array(strArr, VARConfigs1);
// printf("DevName = %s\r\n", VARConfigs1[0].DevName);
// printf("Name = %s\r\n", VARConfigs1[0].VarList.Name);
// printf("Unit=%s\r\n", VARConfigs1[0].VarList.Unit);
// printf("SignalType=%d\r\n", VARConfigs1[0].VarList.SignalType);
// printf("UpRangeValue=%6.3d\r\n", VARConfigs1[0].VarList.UpRangeValue);
// printf("DownRangeValue=%6.3d\r\n", VARConfigs1[0].VarList.DownRangeValue);
// printf("DataType=%d\r\n", VARConfigs1[0].VarList.DataType);
// printf("Decimal=%d\r\n", VARConfigs1[0].VarList.Decimal);
// printf("RegAddr=%d\r\n", VARConfigs1[0].VarList.Param.RegAddr);
// printf("RegType=%d\r\n", VARConfigs1[0].VarList.Param.RegType);
// printf("WriteType=%d\r\n", VARConfigs1[0].VarList.Param.WriteType);
// printf("EventName=%s\r\n", VARConfigs1[0].VarList.Event.EventName);
// printf("MathType=%d\r\n", VARConfigs1[0].VarList.Event.MathType);
// printf("MathValue=%d\r\n", VARConfigs1[0].VarList.Event.MathValue);
printf("/*********************************PAGEConfigs[x]*******************************/\r\n");
T_PAGEConfigs PAGEConfigs[1];
GUI_cJSON_to_struct_array(GUIstrArr, PAGEConfigs);
printf("id = %d\r\n", PAGEConfigs[0].u32Id);
printf("x = %d\r\n", PAGEConfigs[0].u16X);
printf("y = %d\r\n", PAGEConfigs[0].u16Y);
printf("w = %d\r\n", PAGEConfigs[0].u16W);
printf("h = %d\r\n", PAGEConfigs[0].u16H);
printf("variable = %d\r\n", PAGEConfigs[0].date.u32Variable);
printf("value = %s\r\n", PAGEConfigs[0].date.Value);
printf("min = %d\r\n", PAGEConfigs[0].date.u32Min);
printf("max = %d\r\n", PAGEConfigs[0].date.u32Max);
return 0;
}
// parse a key-value pair
int cJSON_to_str(char *json_string, char *str_val)
{
cJSON *root = cJSON_Parse(json_string);
if (!root)
{
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
return -1;
}
else
{
cJSON *item = cJSON_GetObjectItem(root, "firstName");
if (item != NULL)
{
printf("cJSON_GetObjectItem: type=%d, key is %s, value is %s\n", item->type, item->string, item->valuestring);
memcpy(str_val, item->valuestring, strlen(item->valuestring));
}
cJSON_Delete(root);
}
return 0;
}
// parse a object to struct
int cJSON_to_struct(char *json_string, people *person)
{
cJSON *item;
cJSON *root = cJSON_Parse(json_string);
if (!root)
{
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
return -1;
}
else
{
cJSON *object = cJSON_GetObjectItem(root, "person");
if (object == NULL)
{
评论0