c++ json解析

1.写到string

string test_write()
{
    Json::Value root;  // 表示整个 json 对象,实例化一个json对象
    root["platenumber"] = Json::Value("value_string");
    root["platetype"] = Json::Value(0);
    root["snopshotplaceid"] = Json::Value("12345678");
    root["snopshottime"] = Json::Value("20171222");
    root["platenumber"] = Json::Value("123456");
    root["imgpath"] = Json::Value("D:/TEST.JPG");
    root["extFlag"] = Json::Value("EXTFLAG");
    root["taskid"] = Json::Value("TASKID");

    Json::ValueType type = root.type();                       // 获得 root 的类型,此处为 objectValue 类型。

    Json::StyledWriter styled_writer;

    string strJson = GBKToUTF8(styled_writer.write(root));
    const char* chData = strJson.c_str();

    cout << strJson << endl;

    return strJson;
}

  其中多字节下,GBK转UTF8

string GBKToUTF8(const std::string& strGBK)
{
    string strOutUTF8 = "";
    WCHAR * str1;
    int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    str1 = new WCHAR[n];
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    char * str2 = new char[n];
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    strOutUTF8 = str2;
    delete[]str1;
    str1 = NULL;
    delete[]str2;
    str2 = NULL;
    return strOutUTF8;
}

  

2.从string解析json

void test_read(string str)
{
    string str_json = Utf8ToGbk(str.c_str());

    Json::Reader reader;
    Json::Value root;
    if (reader.parse(str_json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素   
    {
        std::cout << "========================[Dispatch]========================" << std::endl;
        std::string plate_number = root["platenumber"].asString();
        int plate_type = root["platetype"].asInt();
        std::string snopshot_place_id = root["snopshotplaceid"].asString();
        std::string snopshot_time = root["snopshottime"].asString();
        std::string image_path = root["imgpath"].asString();
        std::string extFlag = root["extFlag"].asString();
        std::string taskid = root["taskid"].asString();

        cout << "plate_number         :" << plate_number << endl;
        cout << "plate_type           :" << plate_type << endl;
        cout << "snopshot_place_id    :" << snopshot_place_id << endl;
        cout << "snopshot_time        :" << snopshot_time << endl;
        cout << "image_path           :" << image_path << endl;
        cout << "extFlag              :" << extFlag << endl;
        cout << "taskid               :" << taskid << endl;
        cout << endl;
    }
}

  其中,多字节下UTF8转GBK

std::string Utf8ToGbk(const char* utf8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len + 1];
    memset(wstr, 0, len + 1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len + 1];
    memset(str, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if (wstr) delete[] wstr;
    return str;
}

  

3.完整示例

// mytest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <Windows.h>


#include "..\\..\\include\\json\\writer.h" //json解析
#include "..\\..\\include\\json\\reader.h" //json解析

#ifdef _DEBUG
#pragma comment(lib, "..\\..\\build\\vs71\\debug\\lib_json\\json_vc71_libmtd.lib")
#else
#pragma comment(lib, "..\\..\\build\\vs71\\release\\lib_json\\json_vc71_libmt.lib")
#endif

using namespace std;

string GBKToUTF8(const std::string& strGBK)
{
    string strOutUTF8 = "";
    WCHAR * str1;
    int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    str1 = new WCHAR[n];
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
    n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    char * str2 = new char[n];
    WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
    strOutUTF8 = str2;
    delete[]str1;
    str1 = NULL;
    delete[]str2;
    str2 = NULL;
    return strOutUTF8;
}

std::string Utf8ToGbk(const char* utf8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);

    wchar_t* wstr = new wchar_t[len + 1];
    memset(wstr, 0, len + 1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len + 1];
    memset(str, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if (wstr) delete[] wstr;
    return str;
}

string test_write()
{
    Json::Value root;  // 表示整个 json 对象
    root["platenumber"] = Json::Value("value_string");
    root["platetype"] = Json::Value(0);
    root["snopshotplaceid"] = Json::Value("12345678");
    root["snopshottime"] = Json::Value("20171222");
    root["platenumber"] = Json::Value("123456");
    root["imgpath"] = Json::Value("D:/TEST.JPG");
    root["extFlag"] = Json::Value("EXTFLAG");
    root["taskid"] = Json::Value("TASKID");

    Json::ValueType type = root.type();                       // 获得 root 的类型,此处为 objectValue 类型。

    Json::StyledWriter styled_writer;

    string strJson = GBKToUTF8(styled_writer.write(root));
    const char* chData = strJson.c_str();

    cout << strJson << endl;

    return strJson;
}

void test_read(string str)
{
    string str_json = Utf8ToGbk(str.c_str());

    Json::Reader reader;
    Json::Value root;
    if (reader.parse(str_json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素   
    {
        std::cout << "========================[Dispatch]========================" << std::endl;
        std::string plate_number = root["platenumber"].asString();
        int plate_type = root["platetype"].asInt();
        std::string snopshot_place_id = root["snopshotplaceid"].asString();
        std::string snopshot_time = root["snopshottime"].asString();
        std::string image_path = root["imgpath"].asString();
        std::string extFlag = root["extFlag"].asString();
        std::string taskid = root["taskid"].asString();

        cout << "plate_number         :" << plate_number << endl;
        cout << "plate_type           :" << plate_type << endl;
        cout << "snopshot_place_id    :" << snopshot_place_id << endl;
        cout << "snopshot_time        :" << snopshot_time << endl;
        cout << "image_path           :" << image_path << endl;
        cout << "extFlag              :" << extFlag << endl;
        cout << "taskid               :" << taskid << endl;
        cout << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    string strJson = test_write();

    test_read(strJson);

    system("pause");
    return 0;
}

 参考资料:https://www.cnblogs.com/betterwgo/p/8086422.html 

转载于:https://www.cnblogs.com/curo0119/p/8921221.html

C语言中有多种方法可以进行JSON解析。一种常用的方法是使用开源库RapidJSON。RapidJSON是一个只有头文件的C库,使用起来非常方便。你只需要下载并解压后,将include/rapidjson目录拷贝到你的项目中即可开始使用。RapidJSON支持SAX和DOM两种解析方式,SAX解析器将JSON解析为事件序列,而DOM解析器将JSON解析为内存中的树形结构。 另外还有一个流行的C库是JsonCpp,也是用于JSON解析和生成的开源库。JsonCpp与RapidJSON具有很多相似的功能,但也有一些不同之处。你可以根据自己的需求选择使用其中的一个库来进行C语言中的JSON解析。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [json.zip_JSON_c/c++ json_c/c++ json 解析_json解析 c++_解析json](https://download.youkuaiyun.com/download/weixin_42662605/86531966)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C/C++C++Json解析和生成的开源库:RapidJsonJsonCpp](https://blog.youkuaiyun.com/weixin_43729127/article/details/129473932)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值