Tinyjson
Tiny and simple json parser ( header only )
Install / Use
/learn @button-chen/TinyjsonREADME
tinyjson - a simple json parser for C++
Features
- 轻量级
- 跨平台
- Header only
Example
read json:
#include <string>
#include <iostream>
#include <cassert>
#include "tinyjson.hpp"
using namespace std;
using namespace tiny;
string jsonstring = "\
{\
\"name\":\"zhangsan\",\
\"age\" : 26,\
\"data\" : [\
{\
\"one\":\"chenone\",\
\"two\" : {\
\"love1\":\"2233\",\
\"love2\":44444\
}\
}\
]\
}\
";
void TEST1() {
TinyJson json;
json.ReadJson(jsonstring);
string name = json.Get<string>("name");
int age = json.Get<int>("age");
assert(name == "zhangsan");
assert(age == 26);
xarray data = json.Get<xarray>("data");
for (int i = 0; i < data.Count(); i++) {
data.Enter(i);
string one = data.Get<string>("one");
assert(one == "chenone");
xobject two = data.Get<xobject>("two");
for (int ii = 0; ii < two.Count(); ii++) {
two.Enter(ii);
string love1 = two.Get<string>("love1");
int love2 = two.Get<int>("love2");
assert(love1 == "2233");
assert(love2 == 44444);
}
}
cout << "TEST 1 PASS" << endl;
}
write json:
void TEST3() {
TinyJson wjson;
wjson["name"].Set("liergou");
wjson["age"].Set(26);
wjson["handsome"].Set(true);
TinyJson subjson;
subjson["love1"].Set("book");
subjson["love2"].Set(666);
TinyJson subjson2;
subjson2["love1"].Set("book2");
subjson2["love2"].Set(6662);
wjson["data"].Set(subjson);
wjson["data2"].Set(subjson2);
string str = wjson.WriteJson();
cout << "json string: \r\n" << endl;
cout << str << endl;
}
output :
/*
{
"name": "liergou",
"age": 26,
"handsome": true,
"data": {
"love1": "book",
"love2": 666
},
"data2": {
"love1": "book2",
"love2": 6662
}
}
*/
Usage
- windows vs2010 及以后版本, linux 编译需要编译选项: -std=c++0x
- 更多例子见 tinyjson_test.h
Related Skills
node-connect
354.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
112.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
354.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
354.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
