VS2013 配置使用微软开源sdk: C++ REST SDK 及运行官方的 JSON例子

安装微软的开源 cpprestsdk  (C++ REST SDK (codename "Casablanca")),要先有项目;这里新建一个WIN32控制台项目,名为XXX,默认使用系统生成的代码;


然后打开:VS2013 -> 工具 ->库程序包管理器->程序包管理器控制台


输入 :

install-package cpprestsdk


等待安装完毕;

或者慢的话,到 https://www.nuget.org/packages?q=cpprestsdk.v120

手动把这几个包下载下来(点击进去,点download)放到缓存目录: C:\Users\Administrator\AppData\Local\NuGet\Cache


再执行 install-package cpprestsdk

等待安装

显示 

。。。

已成功将“cpprestsdk 2.9.1.1”添加到 xxx (你新建的项目名),则安装成功。


把main文件所在的代码替换成下面例子的代码:

// xx.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

/*
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}
*/

#include <cpprest/http_client.h>
#include <cpprest/json.h>

//#include <http_client.h>
#include <iostream>
//#include <json.h>

using namespace web;
using namespace web::http;
using namespace web::http::client;

using namespace std;

// Retrieves a JSON value from an HTTP request.
pplx::task<void> RequestJSONValueAsync()
{
	// TODO: To successfully use this example, you must perform the request 
	// against a server that provides JSON data. 
	// This example fails because the returned Content-Type is text/html and not application/json.
	//http_client client(L"http://www.fourthcoffee.com");
	http_client client(L"http://www.fourthcoffee.com");
	return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
	{
		if (response.status_code() == status_codes::OK)
		{
			wcout<< response.extract_string().get().c_str()<<endl;
			return response.extract_json();
		}

		// Handle error cases, for now return empty json value...
		return pplx::task_from_result(json::value());
	})
		.then([](pplx::task<json::value> previousTask)
	{
		try
		{
			const json::value& v = previousTask.get();
			// Perform actions here to process the JSON value...
		}
		catch (const http_exception& e)
		{
			// Print error.
			wostringstream ss;
			ss << e.what() << endl;
			wcout << ss.str();
		}
	});

	/* Output:
	Content-Type must be application/json to extract (is: text/html)
	*/
}

// Demonstrates how to iterate over a JSON object.
void IterateJSONValue()
{
	// Create a JSON object.
	json::value obj;
	obj[L"key1"] = json::value::boolean(false);
	obj[L"key2"] = json::value::number(44);
	obj[L"key3"] = json::value::number(43.6);
	obj[L"key4"] = json::value::string(U("str"));

	
	// Loop over each element in the object.
	for (auto iter = obj.as_object().cbegin(); iter != obj.as_object().cend(); ++iter)
	{
		// Make sure to get the value as const reference otherwise you will end up copying
		// the whole JSON value recursively which can be expensive if it is a nested object.
		 
		//const json::value &str = iter->first;
		//const json::value &v = iter->second;

		const auto &str = iter->first;
		const auto &v = iter->second;

		// Perform actions here to process each string and value in the JSON object...
		std::wcout << L"String: " << str.c_str() << L", Value: " << v.serialize() << endl;
	}

	/* Output:
	String: key1, Value: false
	String: key2, Value: 44
	String: key3, Value: 43.6
	String: key4, Value: str
	*/
}

int wmain()
{
	// This example uses the task::wait method to ensure that async operations complete before the app exits. 
	// In most apps, you typically don�t wait for async operations to complete.

	wcout << L"Calling RequestJSONValueAsync..." << endl;
	RequestJSONValueAsync().wait();

	wcout << L"Calling IterateJSONValue..." << endl;
	IterateJSONValue();

	getchar();
}




编译,运行,结果:


.............

d)/*]]>*/</script></body></html>
Calling IterateJSONValue...
String: key1, Value: false
String: key2, Value: 44
String: key3, Value: 43.600000000000001
String: key4, Value: "str"


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值