代码拉取完成,页面将自动刷新
C++版本的json库
软件架构说明
#include <iostream>
#include <string>
#include <stdlib.h>
#include "json.h"
int main()
{
JsonObject json;
std::string jsonStr;
std::string jsonResult;
//1. convert string to json
jsonStr = R"({"name":"zhangsan","age": 18, "gender":"male","weight": 66.3})";
if (json.read_from_string(jsonStr))
{
std::cout << "name: " << json["name"].as_string().c_str() << std::endl;
std::cout << "age: " << json["age"].as_int16() << std::endl;
std::cout << "gender: " << json["gender"].as_string().c_str() << std::endl;
std::cout << "weight: " << json["weight"].as_double() << std::endl;
}
//json to string
if (json.write_to_string(jsonResult))
{
std::cout << "the raw string: " << jsonResult.c_str() << std::endl;
}
json.clear();
//2. json object
json["name"] = "zhangsan";
json["age"] = 18;
json["gender"] = "male";
json["weight"] = 66.3;
//json to string
if (json.write_to_string(jsonResult))
{
std::cout << "the raw string: " << jsonResult.c_str() << std::endl;
}
json.clear();
//3. json array
json[0] = "Monday";
json[1] = "Tuesday";
json[2] = "Wednesday";
json[3] = "Thursday";
json[4] = "Friday";
json[5] = "Saturday";
json[6] = "Sunday";
//json to string
if (json.write_to_string(jsonResult))
{
std::cout << "the week string: " << jsonResult.c_str() << std::endl;
}
for (int i = 0; i < json.array_size(); i++)
{
std::cout << "the " << i << " is:" << json[i].as_string() << std::endl;
}
json.clear();
//4. json array parse
jsonStr = R"(["orange", "banana", "apple"])";
if (json.read_from_string(jsonStr))
{
for (int i = 0; i < json.array_size(); i++)
{
std::cout << "the " << i << " is:" << json[i].as_string() << std::endl;
}
}
json.clear();
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。