卡萨布兰卡传奇继续 - 但我取得了进步 [英] Casablanca saga continues - but i have made progress
本文介绍了卡萨布兰卡传奇继续 - 但我取得了进步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我现在已成功从服务器检索JSON,但在尝试迭代数组时在Visual Studio中收到第一次机会异常
Hi all, I now have succeeded in retrieving JSON from the server but receive a "first chance exception" in Visual Studio when trying to iterate over the array
#include "stdafx.h"
#include <winsock2.h>
#include <string>
#include <time.h>
#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
#pragma comment(lib, "Ws2_32.lib")
// Retrieves a JSON value from an HTTP request.
pplx::task<void> RequestJSONValueAsync(http_request requester)
{
http_client client(L"http://10.10.1.11:9000/jsonrpc.js"); // my local server
return client.request(requester).then([](http_response response) -> pplx::task<json::value>
{
if (response.status_code() == status_codes::OK)
{
json::value jsonValue = response.extract_json().get();
json::value jsonValue2 = jsonValue.at(U("result"));
// result contains an array ( much bigger but you get the picture)
//artists_loop [{"artist":"10,000 Maniacs","id":319},{"artist":"Abb
// ie Lathe","id":596},{"artist":"Adam Harasiewicz","id":321},{"artist":"Adele","id":322}]
json::value ArtistsValue = jsonValue2.at(U("artists_loop")); // artists_loop is the name of the array in result
if (ArtistsValue.size() > 0)
{
// I get a first chance exception here.
for (auto iter = ArtistsValue.as_object().cbegin(); iter != ArtistsValue.as_object().cend(); ++iter)
{
}
}
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.
std::ostringstream ss;
ss << e.what() << std::endl;
std::cout << ss.str();
}
});
}
int wmain()
{
http_request requester;
requester.set_method(methods::POST);
requester.headers().set_content_type(U("application/json"));
requester.set_body("{\"id\":1,\"method\":\"slim.request\",\"params\":[null,[\"artists\",\"0\",\"-1\"]]}\n");
RequestJSONValueAsync(requester).wait();
}
抱歉格式化无法让它变得更好。
我尝试过:
查看有关血腥细节的问题
sorry about the formatting couldn't get it any better.
What I have tried:
See question for the gory details
推荐答案
发现这个
Found this
Taking a look at the changelog for version 2.0.0, you'll find this:
Breaking Change - Changed how iteration over json arrays and objects is performed. No longer is an iterator of std::pair returned. Instead there is a separate iterator for arrays and objects on the json::array and json::object class respectively. This allows us to make performance improvements and continue to adjust accordingly. The array iterator returns json::values, and the object iterator now returns std::pair.
因此我将代码更改为此
so I changed my code to this
for (auto &iter = jsonValue2.as_array().cbegin(); iter != jsonValue2.as_array().cend(); ++iter)
{
json::value v = iter[0];
std::wcout << v.at(U("artist")).as_string() << L": " << v.at(U("id")) << std::endl;
}
一切都很好 - 所以异常试图迭代json :: object而不是json :: array
and all is good - so the exception was trying to iterate over a json::object instead of json::array
这篇关于卡萨布兰卡传奇继续 - 但我取得了进步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文