C++ json解析

2018-06-17 21:27:42来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

利用jsoncpp来做json的解析。

1.jsoncpp下载

    从https://sourceforge.net/projects/jsoncpp/ 下载jsoncpp。makefiles目录下面有VS的工程。

 

2.写到string

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;
}

其中多字节下,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;
}

运行结果:

image

 

完整工程代码:https://gitee.com/betterwgo/jsoncpp_test

测试代码在makefiles目录下的mytest,工程基于VS2013

 

 

 

QQ截图20171205090905

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:LeetCode 1,20,26,27,38,48

下一篇:洛谷P3038 [USACO11DEC]牧草种植Grass Planting