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

48 篇文章 0 订阅
15 篇文章 0 订阅

http://blog.csdn.net/sdsabc2000/article/details/53706632

安装微软的开源 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文件所在的代码替换成下面例子的代码:

  1. // xx.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. /* 
  7. int _tmain(int argc, _TCHAR* argv[]) 
  8. { 
  9.     return 0; 
  10. } 
  11. */  
  12.   
  13. #include <cpprest/http_client.h>  
  14. #include <cpprest/json.h>  
  15.   
  16. //#include <http_client.h>  
  17. #include <iostream>  
  18. //#include <json.h>  
  19.   
  20. using namespace web;  
  21. using namespace web::http;  
  22. using namespace web::http::client;  
  23.   
  24. using namespace std;  
  25.   
  26. // Retrieves a JSON value from an HTTP request.  
  27. pplx::task<void> RequestJSONValueAsync()  
  28. {  
  29.     // TODO: To successfully use this example, you must perform the request   
  30.     // against a server that provides JSON data.   
  31.     // This example fails because the returned Content-Type is text/html and not application/json.  
  32.     //http_client client(L"http://www.fourthcoffee.com");  
  33.     http_client client(L"http://www.fourthcoffee.com");  
  34.     return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>  
  35.     {  
  36.         if (response.status_code() == status_codes::OK)  
  37.         {  
  38.             wcout<< response.extract_string().get().c_str()<<endl;  
  39.             return response.extract_json();  
  40.         }  
  41.   
  42.         // Handle error cases, for now return empty json value...  
  43.         return pplx::task_from_result(json::value());  
  44.     })  
  45.         .then([](pplx::task<json::value> previousTask)  
  46.     {  
  47.         try  
  48.         {  
  49.             const json::value& v = previousTask.get();  
  50.             // Perform actions here to process the JSON value...  
  51.         }  
  52.         catch (const http_exception& e)  
  53.         {  
  54.             // Print error.  
  55.             wostringstream ss;  
  56.             ss << e.what() << endl;  
  57.             wcout << ss.str();  
  58.         }  
  59.     });  
  60.   
  61.     /* Output: 
  62.     Content-Type must be application/json to extract (is: text/html) 
  63.     */  
  64. }  
  65.   
  66. // Demonstrates how to iterate over a JSON object.  
  67. void IterateJSONValue()  
  68. {  
  69.     // Create a JSON object.  
  70.     json::value obj;  
  71.     obj[L"key1"] = json::value::boolean(false);  
  72.     obj[L"key2"] = json::value::number(44);  
  73.     obj[L"key3"] = json::value::number(43.6);  
  74.     obj[L"key4"] = json::value::string(U("str"));  
  75.   
  76.       
  77.     // Loop over each element in the object.  
  78.     for (auto iter = obj.as_object().cbegin(); iter != obj.as_object().cend(); ++iter)  
  79.     {  
  80.         // Make sure to get the value as const reference otherwise you will end up copying  
  81.         // the whole JSON value recursively which can be expensive if it is a nested object.  
  82.            
  83.         //const json::value &str = iter->first;  
  84.         //const json::value &v = iter->second;  
  85.   
  86.         const auto &str = iter->first;  
  87.         const auto &v = iter->second;  
  88.   
  89.         // Perform actions here to process each string and value in the JSON object...  
  90.         std::wcout << L"String: " << str.c_str() << L", Value: " << v.serialize() << endl;  
  91.     }  
  92.   
  93.     /* Output: 
  94.     String: key1, Value: false 
  95.     String: key2, Value: 44 
  96.     String: key3, Value: 43.6 
  97.     String: key4, Value: str 
  98.     */  
  99. }  
  100.   
  101. int wmain()  
  102. {  
  103.     // This example uses the task::wait method to ensure that async operations complete before the app exits.   
  104.     // In most apps, you typically don�t wait for async operations to complete.  
  105.   
  106.     wcout << L"Calling RequestJSONValueAsync..." << endl;  
  107.     RequestJSONValueAsync().wait();  
  108.   
  109.     wcout << L"Calling IterateJSONValue..." << endl;  
  110.     IterateJSONValue();  
  111.   
  112.     getchar();  
  113. }  
// 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"

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值