使用jsoncpp解析生成json


此站點下載jsoncpp(https://sourceforge.net/projects/jsoncpp/這個站點的版本較舊)

在電腦上安裝Python,運行amalgamate.py,生成的dist目錄中包含了我們需要的源文件和頭文件

 

以下是使用jsoncpp解析與生成json的示例代碼,

其中json_text.txt的內容為(關於此json數據的來源可參考此站點

{
	"face":
	[
		{
			"attribute":
			{
				"age":27,
				"gender":"Male",
				"lefteye_opendegree":43,
				"mouth_opendegree":2,
				"pose":
				{
					"raise":-21,
					"tilting":-2,
					"turn":4
				},
				"righteye_opendegree":47
			},
			"face_id":"83e3edac3d5d49579089d3ee043d41ec",
			"position":
			{
				"center":{"x":179,"y":50},
				"eye_left":{"x":206,"y":78},
				"eye_right":{"x":248,"y":78},
				"height":94,"width":94
			},
			"tag":""
		}
	],
	"img_height":500,
	"img_id":"030-3aff983ea4ea5a35f89ae6",
	"img_width":350,
	"res_code":"0000",
	"url":"http://e.hiphotos.baidu.com/baike/pic/item/9345d688d43f879428d347b3d81b0ef41bd53a7a.jpg"
}

 jsoncpp_parse_test.cpp

 

#include <iostream>
#include <fstream>
#include "jsoncpp/json.h"
int main()
{
    using namespace std;
    using namespace Json;
    ifstream ifs("json_text.txt");
    if (ifs)
    {
        string line, jsonStr;
        while (getline(ifs, line))
            jsonStr.append(line);
        CharReader *reader = CharReaderBuilder().newCharReader();
        Value root;
        JSONCPP_STRING errs;
        if (reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.size(), &root, &errs))
        {
            auto faceId = root["face"][0]["face_id"].asString();
            auto imgWidth = root["img_width"].asInt();
            auto imgHeight = root["img_height"].asInt();
            auto resCode = root["res_code"].asString();
            auto url = root["url"].asString();
            cout << "face_id:\t" << faceId << endl;
            cout << "img_width:\t" << imgWidth << endl;
            cout << "img_height:\t" << imgHeight << endl;
            cout << "res_code:\t" << resCode << endl;
            cout << "url:\t\t" << url << endl;
        }
        else
            cout << errs << endl;
        delete reader;
    }
    return 0;
}

 

jsoncpp_generate_test.cpp

 

#include <iostream>
#include <memory>
#include "jsoncpp/json.h"
int main()
{
    std::unique_ptr<Json::StreamWriter> writer(Json::StreamWriterBuilder().newStreamWriter());
    Json::Value array, element;
    element["face_id"] = "a9cebf8d5ae6fff514d8d2d8e07fa55b";
    element["similarity"] = 100;
    element["img_id"] = "12.jpg";
    element["people_name"] = "張藝謀";
    array["result"].append(element);
    element["face_id"] = "7f2de0e85bede3171c839d0dcabd849f";
    element["similarity"] = 55.379097;
    element["img_id"] = "6.jpg";
    element["people_name"] = "曾國藩";
    array["result"].append(element);
    element["face_id"] = "40ebb31e8af7237a73dec9f242885a7e";
    element["similarity"] = 52.59766;
    element["img_id"] = "2.jpg";
    element["people_name"] = "測試";
    array["result"].append(element);
    //std::ostringstream ss;
    if (writer->write(array, &std::cout))
        std::cout << "write failed" << std::endl;
    else
        std::cout << std::endl;
    return 0;
}

 

Compile.bat

 

g++ -std=c++11 jsoncpp_parse_test.cpp jsoncpp/jsoncpp.cpp -o jsoncpp_parse_test
g++ -std=c++11 jsoncpp_generate_test.cpp jsoncpp/jsoncpp.cpp -o jsoncpp_generate_test

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM