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

来源:互联网 发布:阿里云cdn怎么关闭 编辑:程序博客网 时间:2024/06/15 23:43

安装微软的开源 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: falseString: key2, Value: 44String: key3, Value: 43.6String: 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 0