Click here to Skip to main content
15,888,102 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

C++
#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
Posted
Comments
Sascha Lefèvre 17-Feb-16 5:27am    
There won't be a Casablanca continuation :-/
pkfox 17-Feb-16 6:30am    
Eh ? has development stopped ?
Sascha Lefèvre 17-Feb-16 6:34am    
Sorry, I was referring to the movie ;-)
CPallini 17-Feb-16 5:34am    
You should report the exact exception message.

1 solution

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

C++
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;
					
				}


and all is good - so the exception was trying to iterate over a json::object instead of json::array
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900