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"

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

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)
{
    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);
        int i = 0;
        char *p  = NULL; 
        cJSON *it; 
        for(i=0; i< railwayFence_array_size; i++) {  
            cJSON *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);
            int j = 0;
            char *q  = NULL; 
            cJSON *jt;  
            for(j=0; j< Coordinate_array_size; j++) {
                cJSON *CoordinateItem = cJSON_GetArrayItem(CoordinateArray,j);
                q = cJSON_PrintUnformatted(CoordinateItem);  
                jt = cJSON_Parse(q); 
                if(!jt)  
                    continue ;

                cJSON *B,*L;
                B = cJSON_GetObjectItem(jt, "B");  
                printf("B is %f\n",B->valuedouble);  
                L = cJSON_GetObjectItem(jt, "L");  
                printf("L is %f\n",L->valuedouble);
            
                free(q); cJSON_Delete(jt);                
            }
            free(p); cJSON_Delete(it);
        }
        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解析json文件

main.js

var fs = require("fs");
var contents = fs.readFileSync("railwayFence.json");
var obj = JSON.parse(contents);
//console.log("<<<<<<<<<<<<<<<<<<<<"+JSON.stringify(obj));
for(var i in obj.railwayFence){
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>put areaName: ' + obj.railwayFence[i].areaName)
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>put dangerTypeName: ' + obj.railwayFence[i].danggerTypeName)
    for(var j in obj.railwayFence[i].coordinates){
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>put B: ' + obj.railwayFence[i].coordinates[j].B)
        console.log('>>>>>>>>>>>>>>>>>>>>>>>>put L: ' + obj.railwayFence[i].coordinates[j].L)            
    }
}

 

 

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

 

posted @ 2018-05-24 08:56  dong1  阅读(1518)  评论(0编辑  收藏  举报