C语言读取json文件和C++语言读取json文件

本博客主要记录基于C++语言json文件的读取,代码已在windows10平台visual studio2019运行,也在linux平台编译运行。

C语言读取json文件,仅在windows10上运行,未在linux运行(有点懒了)(添加:20221009)。

 

本代码链接:

链接:https://pan.baidu.com/s/1ujX19IUV0EPSIMyIcBnClA?pwd=r63z 
提取码:r63z

名称:读取json(C++语言(windows与linux平台)C语言(windows平台)).zip 文件

说明:读取json代码无需下载其它库,可直接下载链接中的代码,即可运行。

 一.C++ 读取json文件

json文件:

{
   "age" : 21,
   "name" : "shuiyixin",
   "sex" : "man",
   "friends" : {
      "friend_age" : 21,
      "friend_name" : "ZhaoWuxian",
      "friend_sex" : "man"
   },
   "hobby" : [ "sing", "run", "Tai Chi" ] 
}

 

主文件(Json).cpp

// Json.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "json.h"
#include <fstream>
using namespace std;
void readFileJson(string json_root)
{
    Json::Reader reader;
    Json::Value root;
    //从文件中读取,保证当前文件有demo.json文件  
    ifstream input(json_root, ios::binary);
    if (!input.is_open())
    {
        cout << "Error opening file\n";
        return;
    }
    if (reader.parse(input, root))
    {
        //读取根节点信息  
        string name = root["name"].asString();
        int age = root["age"].asInt();
        string sex = root["sex"].asString();
        //读取子节点信息  
        string friend_name = root["friends"]["friend_name"].asString();
        int friend_age = root["friends"]["friend_age"].asInt();
        string friend_sex = root["friends"]["friend_sex"].asString();
        //读取数组信息 
        string info="hoppy:\t[";
        for (unsigned int i = 0; i < root["hobby"].size(); i++)
        {
            string ach = root["hobby"][i].asString();
            info = info + ach + "," + "\t";
        }
        info = info + "]";
        cout << "{\n age:" << age << ",\n name:" << name << ",\n sex:" << sex << ",\n friends:{\n" << "\tfriend_age:" << friend_age << ",\n\tfridend_name:" << friend_name <<
            ",\n\tfriend_sex:" << friend_sex << "\n\t},\n"<<info<<"\n}"<<endl;  
    }

    input.close();
}
int main()
{
    string json_root = "test.json";
    readFileJson(json_root); 
    return 0;
}

CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.6)
project(readjson)
add_definitions(-std=c++11)
include_directories(${PROJECT_SOURCE_DIR})
#link_directories(/usr/local/cuda/lib64) #连接库文件
add_executable(readjson ${PROJECT_SOURCE_DIR}/Json.cpp
    ${PROJECT_SOURCE_DIR}/jsoncpp.cpp
    )
#target_link_libraries(yolo ${OpenCV_LIBS})
#target_link_libraries(yolo /home/ubuntu/soft/TensorRT-8.2.5.1/lib/stubs/libnvonnxparser.so)

 

代码链接文件格式:

 

 

linux平台代码运行结果:

 

 

 

 二.C 读取json文件(添加:20221009)

json文件:

{
"server_ip":"192.168.100.104",
"server_port":"6666", 

"rtsp_id":"0",
"rtsp_link":"rtsp://192.168.100.101:554",
"rtsp_user":"admin",
"rtsp_password":"ubuntu123456",
"rtsp_encodeType":"7",
"rtsp_buffersize":"8294400"
	
}

主文件(Json).c

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "ReadJson.h"

/* Read a file, parse, render back, etc. */
char* dofile(char* filename)
{
    FILE* f; 
    long length; 
    char* data;

    f = fopen(filename, "rb"); 
    fseek(f, 0, SEEK_END); 
    length = ftell(f); 
    fseek(f, 0, SEEK_SET);
    data = (char*)malloc(length + 1); 
    fread(data, 1, length, f); 
    fclose(f);
    //doit(data);
    //parseJsonText(data);
    //free(data);
    return data;
}
char* process_variate(char* data,char* variate) {
    char* out;
    cJSON* json;
    cJSON* childJson = NULL;

    json = cJSON_Parse(data);
    childJson = cJSON_GetObjectItem(json, variate);
    char* result = childJson->valuestring;
    return result;
}


int main(int argc, const char* argv[]) {
    char* file_name = "parameters.json";
    char* data;
    char* variate;
    data=dofile(file_name);
    char* server_ip=process_variate( data, "server_ip");
    char* server_port_temp = process_variate(data, "server_port");
    int server_port = atoi(server_port_temp);
    
    char* rtsp_id_temp = process_variate(data, "rtsp_id");
    int rtsp_id = atoi(rtsp_id_temp);
    char* rtsp_link = process_variate(data, "rtsp_link");
    char* rtsp_user = process_variate(data, "rtsp_user");
    char* rtsp_password = process_variate(data, "rtsp_password");
    char* rtsp_encodeType_temp = process_variate(data, "rtsp_encodeType");
    int rtsp_encodeType = atoi(rtsp_encodeType_temp);
    char* rtsp_buffersize_temp = process_variate(data, "rtsp_buffersize");
    int rtsp_buffersize = atoi(rtsp_buffersize_temp);

    free(data);

    return 0;
}

 

结果:

 

代码也可在github上下载:https://github.com/tangjunjun966/ReadJson-c-cpp-

posted @ 2022-10-08 21:14  tangjunjun  阅读(3921)  评论(0编辑  收藏  举报
https://rpc.cnblogs.com/metaweblog/tangjunjun