json文件解析

1、应用c语言cjson库来解析

main.c
/*
        reference documentation
        https://blog.csdn.net/stsahana/article/details/79638992
        https://blog.csdn.net/fengxinlinux/article/details/53121287
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "cJSON.h"

#define RAILWAY_MAX_NUM 10
#define POINT_MAX_NUM 1024

char *json_file_path = "./railwayFence.json";

typedef struct{
    double x;
    double y;
}Point_t;

typedef struct
{
    double B;
    double L;
    double H;
}Coordinate_t;

typedef struct {
  char *areaName;
    //Coordinate_t Coordinate[POINT_MAX_NUM];
    Point_t Point[POINT_MAX_NUM];
    char *dangerTypeName;
    int dangerType;
    int type;
    int priority;
    char *railwayName;
    char *comment;
    int color;
} railwayFence_t;

typedef struct {
  railwayFence_t railwayFence[RAILWAY_MAX_NUM];
} warn_offline_t;
warn_offline_t warn_offline;

int arrs[POINT_MAX_NUM/4];

char *json_loader(char *path)
{
    FILE *f;
    long len;
    char *content;
        f=fopen(path,"rb");
        fseek(f,0,SEEK_END);
        len=ftell(f);
        fseek(f,0,SEEK_SET);
        content=(char*)malloc(len+1);
        fread(content,1,len,f);
        fclose(f);
        return content;
}

int main(void)
{
        //railwayFence
        int i = 0;  
        cJSON *railwayFenceItem;
        char *p  = NULL; 
        cJSON *it; 

        //Coordinate
        int j = 0;  
        cJSON *CoordinateItem; 
        char *q  = NULL; 
        cJSON *jt;
        cJSON *B,*L;

        char *json_str = json_loader(json_file_path);

        cJSON *root=cJSON_Parse(json_str);
        if (!root) {
                printf("Error before: [%s]\n",cJSON_GetErrorPtr());
        }

        //railwayFence array
        cJSON *railwayFenceArray = cJSON_GetObjectItem(root, "railwayFence");  
        if(!railwayFenceArray){  
                printf("Error before: [%s]\n",cJSON_GetErrorPtr()); 
        }
        int railwayFence_array_size = cJSON_GetArraySize(railwayFenceArray);  
        printf("railwayFence array size is %d\n",railwayFence_array_size);
  
        for(i=0; i< railwayFence_array_size; i++) {  
                railwayFenceItem = cJSON_GetArrayItem(railwayFenceArray,i);
                p = cJSON_PrintUnformatted(railwayFenceItem);  
            it = cJSON_Parse(p); 
                if(!it)  
            continue ;

                cJSON *areaName,*dangerType;
                areaName = cJSON_GetObjectItem(it, "areaName");  
                printf("areaName is %s\n",areaName->valuestring);  
                dangerType = cJSON_GetObjectItem(it, "dangerType");  
                printf("dangerType is %s\n",dangerType->valuestring);

                //Coordinate array
                cJSON *CoordinateArray = cJSON_GetObjectItem(railwayFenceItem, "coordinates");  
                if(!CoordinateArray){  
                        printf("Error before: [%s]\n",cJSON_GetErrorPtr()); 
                }
                int Coordinate_array_size = cJSON_GetArraySize(CoordinateArray);  
                printf("Coordinate array size is %d\n",Coordinate_array_size);  
                for(j=0; j< Coordinate_array_size; j++) {

                        CoordinateItem = cJSON_GetArrayItem(CoordinateArray,j);

                        q = cJSON_PrintUnformatted(CoordinateItem);  
                        jt = cJSON_Parse(q); 
                        if(!jt)  
                        continue ;
                    
                        B = cJSON_GetObjectItem(jt, "B");  
                        printf("B is %f\n",B->valuedouble);  
                        L = cJSON_GetObjectItem(jt, "L");  
                        printf("L is %f\n",L->valuedouble);

                        warn_offline.railwayFence[i].Point[j].x= B->valuedouble;
                        warn_offline.railwayFence[i].Point[j].y= L->valuedouble;
                
                }
        }

        if(root) { 
                cJSON_Delete(root);  
                return 0;  
        }

    return 0;

}

 Makefile

OBJ= main 
all: ${OBJ}
main:
    gcc -g -o main main.c cJSON.c -lm

clean:
    rm -f ${OBJ}
.PHONY: ${OBJ}

2、nodejs解析

var refStruct = require('ref-struct');
var refArray = require('ref-array');
var ref = require('ref');

var Coordinate_t = refStruct({
    'B':ref.types.double,
    'L':ref.types.double,
});
var CoordinateStructArrayType = refArray(Coordinate_t);
var Coordinate_info = CoordinateStructArrayType(1000);

var warn_info_t = refStruct({
    'areaName':'string',
    'dangerTypeName':'string',
});
var warnStructArrayType = refArray(warn_info_t);
var warn_info = warnStructArrayType(10);]

var obj = JSON.parse(des3_decode(data));
console.log("<<<<<<<<<<<<<<<<<<<<"+JSON.stringify(obj));
for(var i in obj.railwayFence){
        warn_info[i].areaName = obj.railwayFence[i].areaName
        warn_info[i].dangerTypeName = obj.railwayFence[i].danggerTypeName

        console.log('>>>>>>>>>>>>>>>>>>>>>>>>put areaName: ' + warn_info[i].areaName)
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>put dangerTypeName: ' + warn_info[i].dangerTypeName)

        for(var j in obj.railwayFence[i].Coordinate){
                Coordinate_info[j].B = obj.railwayFence[i].Coordinate[j].B;
                Coordinate_info[j].L = obj.railwayFence[i].Coordinate[j].L;

                console.log('>>>>>>>>>>>>>>>>>>>>>>>>put B: ' + Coordinate_info[j].B)
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>put L: ' + Coordinate_info[j].L)            
        }
}

是不是感觉写c语言是在浪费生命!

猜你喜欢

转载自www.cnblogs.com/dong1/p/9081009.html