1 Star 2 Fork 1

Huoty/cppstudy

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_jsoncpp.cpp 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
Huoty 提交于 2024-12-08 23:34 +08:00 . update at 20241208233432
/*
* Copyright (c) JoinQuant Development Team
* Author: Huayong Kuang <kuanghuayong@joinquant.com>
*
*/
#include <iostream>
#include <string>
#include <cstring>
#include <json/value.h>
#include <json/json.h>
using namespace std;
void readStrJson()
{
//字符串
const char* str =
"{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
"\"born\":-100,\"died\":-44}";
/*
//json内容如下:
{
"praenomen":"Gaius",
"nomen":"Julius",
"cognomen":"Caezar",
"born":-100,
"died":-44
}
*/
Json::Reader reader;
Json::Value root;
//从字符串中读取数据
if(reader.parse(str,root))
{
string praenomen = root["praenomen"].asString();
string nomen = root["nomen"].asString();
string cognomen = root["cognomen"].asString();
int born = root["born"].asInt();
int died = root["died"].asInt();
string test = root["test"].asString(); // 没有时返回空字符串 string("")
int test2 = root["test2"].asInt(); // 没有时返回 0
cout << praenomen + " " + nomen + " " + cognomen
<< " was born in year " << born
<< ", died in year " << died << endl;
cout << "test: '" << test << "'" << endl;
cout << "test2: '" << test2 << "'" << endl;
}
//for (int i = 0; i < 10; i++) {
//Json::Value root;
//root["test"] = "hello world";
//}
}
typedef struct {
char name[16];
int age;
} MyT;
void testValue() {
Json::Value jv;
jv["test"] = "hello world";
jv["arr"].append(1);
cout << jv.toStyledString() << endl;
MyT person;
strcpy(person.name, "Huoty");
person.age = 30;
//jv["person"] = person;
}
int main(void)
{
readStrJson();
testValue();
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/konghy/cppstudy.git
git@gitee.com:konghy/cppstudy.git
konghy
cppstudy
cppstudy
master

搜索帮助