先下载源码https://github.com/open-source-parsers/jsoncpp


解压后 到makefiles\vs71,运行jsoncpp.sln,直接编译release版本

注意下面设置,如果不设置的话 会出现json_writer.asm': 错误

然后在 lib_json 工程上点击右键,选在 Property 项,然后再 lib_json Property Pages 对话框上选择 Configuration Properties -> C/C++ -> Output Files, 然后再右边的 Assembler Output 项的值是“Assembly With Source Code (/FAs)”

如果还出现其他的链接错误,记着查看 编译 jsoncpp时   C/C++--》代码生成--》运行库,和引用jsoncpp**.lib的工程中,两个运行库的选项是否一致

小例子

#include <windows.h>
#include <string>
#include <iostream>
#include "json.h"
using namespace std;
#pragma comment(lib, "json_vc71_libmt.lib")

int main()
{

	string strValue = "{\"res\":\"success\",\"info\":\"{\\\"port\\\":1024,\\\"host\\\":\\\"192.168.5.215\\\"}\"}";
	cout << strValue << endl;
	Json::Reader reader;
	Json::Value value;
	if (reader.parse(strValue, value))
	{
		cout << value["res"].asString() << endl;
		cout << value["info"].asString() << endl;
		Json::Reader readerTemp;
		Json::Value valueTemp;
		if (readerTemp.parse(value["info"].asString(), valueTemp))
		{
			cout << valueTemp["host"].asString() << endl;
			cout << valueTemp["port"].asInt() << endl;

		}
	}

	Json::Value root;
	Json::FastWriter writer;
	Json::Value insertObj;
	Json::Value arrayObj;
	Json::Value item,item2;
	Json::Value iNum;
	item[0] = "this is a string";
    item[1] = 999;
	item[2] = "bbbbbb";
	//数组
	string str = writer.write(item);
	//字符串形式
	item2["string"] = "this is a string";
	item2["number"] = 999;
	item2["aaaaaa"] = "bbbbbb";
	arrayObj.append(item);
	string outArray = arrayObj.toStyledString();

	//增加字符串
	root["name"] = "json";
	//增加数字
	root["number"] = 666;
	//增加布尔变量
	root["value"] = true;
	//增加对象数组
	root["array"] = arrayObj;
	string strRoot = root.toStyledString();
	getchar();
	return 0;
}



Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐