2016-06-28 68 views
2

我正在嘗試製作一個程序,它可以在控制檯中用C++的REST API顯示JSON文件。我想從api.trello.com但每一個例子,我遇到給我一個錯誤,通常大約cbegin() & cend()以及它是如何不是web::json::value值...C++當試圖從Web顯示JSON文件時,給我一個錯誤

這裏是JSON文件我代碼:

// The code includes the most frequently used includes necessary to work with C++ REST SDK 
#include "cpprest/containerstream.h" 
#include "cpprest/filestream.h" 
#include "cpprest/http_client.h" 
#include "cpprest/json.h" 
#include "cpprest/producerconsumerstream.h" 
#include <iostream> 
#include <sstream> 
#include <stdio.h> 
#include <stdlib.h> 

using namespace ::pplx; 
using namespace utility; 
using namespace concurrency::streams; 

using namespace web; 
using namespace web::http; 
using namespace web::http::client; 
using namespace web::json; 
using namespace std; 


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

// 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"website.com/theRealURLContainsSecretKeys"); 
    return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value> 
    { 
     if (response.status_code() == status_codes::OK) 
     { 
      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.cbegin(); iter != obj.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; 

     // Perform actions here to process each string and value in the JSON object... 
     std::wcout << L"String: " << str.as_string() << L", Value: " << v.to_string() << 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(); 
    system("pause"); 
} 

我有在VS這個錯誤2015年

的唯一錯誤是在IterateJSONValue()

什麼是我的問題,我該如何解決?

+0

[C++ REST SDK(卡薩布蘭卡)網絡:: JSON迭代]的可能的複製(http://stackoverflow.com/questions/31674575/c-rest-sdk-casablanca-webjson-iteration) –

+0

我發現問題。我正在使用不同版本的REST API,然後顯示了該示例。我剛剛發現了一個完全不同的工作代碼 –

回答

5

json::value不包含成員函數cbegin()。如果你訪問obj.as_object()obj.as_array()你會發現你的開始/結束成員。

// Loop over each element in the object. 
for (const auto &pr : obj.as_object()) { 
    std::wcout << L"String: " << pr.first << L", Value: " << pr.second << endl; 
} 
相關問題