新版jsoncpp使用方法(1.9.4)

官方示例1.9.4
readFromStream

  Json::Value root;
  std::ifstream ifs;
  ifs.open(argv[1]);

  Json::CharReaderBuilder builder;
  builder["collectComments"] = true;
  JSONCPP_STRING errs;
  if (!parseFromStream(builder, ifs, &root, &errs)) {
    std::cout << errs << std::endl;
    return EXIT_FAILURE;
  }
  std::cout << root << std::endl;

readFromString

  const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
  const auto rawJsonLength = static_cast<int>(rawJson.length());
  constexpr bool shouldUseOldWay = false;
  JSONCPP_STRING err;
  Json::Value root;

  if (shouldUseOldWay) {
    Json::Reader reader;
    reader.parse(rawJson, root);
  } else {
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
                       &err)) {
      std::cout << "error" << std::endl;
      return EXIT_FAILURE;
    }
  }
  const std::string name = root["Name"].asString();
  const int age = root["Age"].asInt();

  std::cout << name << std::endl;
  std::cout << age << std::endl;

streamWrite

  Json::Value root;
  Json::StreamWriterBuilder builder;
  const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());

  root["Name"] = "robin";
  root["Age"] = 20;
  writer->write(root, &std::cout);

stringWrite

  Json::Value root;
  Json::Value data;
  constexpr bool shouldUseOldWay = false;
  root["action"] = "run";
  data["number"] = 1;
  root["data"] = data;

  if (shouldUseOldWay) {
    Json::FastWriter writer;
    const std::string json_file = writer.write(root);
    std::cout << json_file << std::endl;
  } else {
    Json::StreamWriterBuilder builder;
    const std::string json_file = Json::writeString(builder, root);
    std::cout << json_file << std::endl;
  }

下面是我写的

// jsoncpp_test.cpp : 定义控制台应用程序的入口点。
//
//更新时间2020.02.18
#include "stdafx.h"
#include "map"
#include <string>
#include "windows.h"
#include "Json.h"

int main()
{
	//写入json文件
	Json::StreamWriterBuilder WriteBuilder;
	auto_ptr<Json::StreamWriter> writer(WriteBuilder.newStreamWriter());
	//Json::CharReader* reader(b.newCharReader());
	Json::Value jsonLine;
	jsonLine["qqq"] = 111;
	jsonLine["www"] = 222;
	//jsonLine["eee"] = 333;//不能这样使用,会报错,名称[""][""]必须完整
	jsonLine["eee"]["aaa"] = 333;
	jsonLine["eee"]["bbb"] = 444;
	ofstream outtofile;
	outtofile.open("F:\\json\\jsoncpp_test\\Debug\\test.json");
	writer->write(jsonLine, &outtofile);
	outtofile.close();

	//test.json内容如下
	//{
	//	"eee" :
	//	{
	//		"aaa" : 333,
	//		"bbb" : 444
	//	},
	//		"qqq" : 111,
	//		"www" : 222
	//}


	//读取json文件内容
	//Json::Value jsonLine;
	Json::CharReaderBuilder readbuilder;
	auto_ptr<Json::CharReader> reader(readbuilder.newCharReader());
	//Json::CharReader *reader = builder.newCharReader();
	JSONCPP_STRING errs;
	ifstream is;
	is.open("F:\\json\\jsoncpp_test\\Debug\\test.json");
	if (Json::parseFromStream(readbuilder, is, &jsonLine, &errs)  && is.is_open())
	{
		string a= jsonLine["qqq"].asString();
		int b = jsonLine["www"].asInt();
		double c = jsonLine["eee"]["aaa"].asDouble();
		float d = jsonLine["eee"]["bbb"].asFloat();
	}
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值