lundi 23 mai 2016

Json value is null from previousTask.get()

I am trying to parse out weather data from this site http://ift.tt/25iMnzX. I am using a service from google to convert the xml into json. Here is the main function of my code that makes the call:

pplx::task<void> RequestJSONValueAsync()
{
    http_client client(U("http://ift.tt/Q55qZD"));
    uri_builder builder(U("/ajax/services/feed/load?v=2.0"));
    builder.append_query(U("q"), U("http://ift.tt/25iMnzX"));

    return client.request(methods::GET, builder.to_string()).then([]    (http_response response) -> pplx::task<json::value>
    {
        if (response.status_code() == status_codes::OK)
        {
            auto body = response.extract_string().get();

            json::value t = json::value::parse(body);

            if (t.is_null())
            {
                wcout << "t is null" << endl;
            }
            else
            {
                wcout << "t is not null" << endl;

                OutputJson(t);
            }

            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
        {
            wcout << "\n\nAttempt with previous task ...\n\n" << endl;
            const json::value& v = previousTask.get();

            if (v.is_null())
            {
                wcout << "v is null" << endl;
            }
            else
            {
                wcout << "v is not null" << endl;
            }

        }
        catch (const json_exception& e)
        {
            printf("Error exception:%s\n", e.what());
        }
    });
}

For whatever reason the v is null, however once the response comes in and I parse the string into a json::value I am able to work with it and it is not null. Does anyone know why the json::value is null in the

.then([](pplx::task<json::value> previousTask) {}

case?

Thanks

Aucun commentaire:

Enregistrer un commentaire