使用JSON解析问题警告 [英] Parsing issue warning with JSON

查看:1461
本文介绍了使用JSON解析问题警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网站读取JSON数据.我在Windows 10上使用具有MinGW编译器的Dev C ++.这是我试图在静态项目中运行的教程中的JSON解析器:

I'm trying to read JSON data from a website. I'm using Dev C++ on Windows 10 with a mingw compiler. This is a JSON parser from a tutorial I'm trying to run in a static project:

#define CURL_STATICLIB
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <sstream>

#include "curl.h"
#include "json.h"

namespace
{
    std::size_t callback(
            const char* in,
            std::size_t size,
            std::size_t num,
            std::string* out)
    {
        const std::size_t totalBytes(size * num);
        out->append(in, totalBytes);
        return totalBytes;
    }
}

int main()
{
    const std::string url("http://date.jsontest.com/");

    CURL* curl = curl_easy_init();

    // Set remote URL.
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    // Don't bother trying IPv6, which would increase DNS resolution time.
    curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    // Don't wait forever, time out after 10 seconds.
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

    // Follow HTTP redirects if necessary.
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    // Response information.
    int httpCode(0);
    // std::unique_ptr<std::string> httpData(new std::string());

    std::stringstream httpData;

    // Hook up data handling function.
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);

    // Hook up data container (will be passed as the last parameter to the
    // callback handling function).  Can be any pointer type, since it will
    // internally be passed as a void pointer.
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData);

    // Run our HTTP GET command, capture the HTTP response code, and clean up.
    curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
    curl_easy_cleanup(curl);

    if (httpCode == 200)
    {
        std::cout << "\nGot successful response from " << url << std::endl;

        // Response looks good - done using Curl now.  Try to parse the results
        // and print them out.
        Json::Value jsonData;
        Json::CharReaderBuilder jsonReader;
        std::string errs;

        if (Json::parseFromStream(jsonReader, httpData, &jsonData, &errs))
            // jsonReader.parse(httpData, jsonData))
        {
            std::cout << "Successfully parsed JSON data" << std::endl;
            std::cout << "\nJSON data received:" << std::endl;
            std::cout << jsonData.toStyledString() << std::endl;

            const std::string dateString(jsonData["date"].asString());
            const std::size_t unixTimeMs(
                    jsonData["milliseconds_since_epoch"].asUInt64());
            const std::string timeString(jsonData["time"].asString());

            std::cout << "Natively parsed:" << std::endl;
            std::cout << "\tDate string: " << dateString << std::endl;
            std::cout << "\tUnix timeMs: " << unixTimeMs << std::endl;
            std::cout << "\tTime string: " << timeString << std::endl;
            std::cout << std::endl;
        }
        else
        {
            std::cout << "Could not parse HTTP data as JSON" << std::endl;
            std::cout << "HTTP data was:\n" << httpData.str() << std::endl;
            return 1;
        }
    }
    else
    {
        std::cout << "Couldn't GET from " << url << " - exiting" << std::endl;
        return 1;
    }

    return 0;
}

这是我得到的错误:

[警告]不推荐使用阅读器"(声明于 C:/Users/me/Downloads/jsoncpp-master/jsoncpp-master/include/json/reader.h:35): |改用CharReader和CharReaderBuilder [-Wdeprecated-clarations]

[Warning] 'Reader' is deprecated (declared at C:/Users/me/Downloads/jsoncpp-master/jsoncpp-master/include/json/reader.h:35): | Use CharReader and CharReaderBuilder instead [-Wdeprecated-declarations]

我发现语法较旧,因此我将对象的术语换成推荐的术语.现在我得到这个错误: 这是我更正的块:

I figured the syntax was older, thus I swapped the object's terms for the recommended ones. Now I'm getting this error: This was my corrected block:

Json::Value jsonData;
Json::CharReaderBuilder CharReader;

if (CharReader.parse(*httpData, jsonData))

这是新的错误:

错误:类Json :: CharReaderBuilder"没有名为"parse"的成员 如果(CharReader.parse(* httpData,jsonData))

error: 'class Json::CharReaderBuilder' has no member named 'parse' if (CharReader.parse(*httpData, jsonData))


上面的代码纠正了这个问题.特定问题已得到纠正. 但是,由于库问题,这是我在编译时遇到的新错误.


The above code corrects this. The particular issue has been corrected. However, due to a library issue, this is my new errors when compiling.

g++.exe main.o -o CurlProject1.exe -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -L"C:/Users/me/Downloads/curl-7.56.1-win64-mingw/lib" -static-libgcc -L"C:/Users/me/Downloads/curl-7.56.1-win64-mingw/lib" ../../Downloads/curl-7.56.1-win64-mingw/lib/libcurl.a ../../Downloads/curl-7.56.1-win64-mingw/lib/libcurl.dll.a

main.o:main.cpp:(.text+0x207): undefined reference to `Json::Value::Value(Json::ValueType)'
main.o:main.cpp:(.text+0x213): undefined reference to `Json::CharReaderBuilder::CharReaderBuilder()'
main.o:main.cpp:(.text+0x242): undefined reference to `Json::parseFromStream(Json::CharReader::Factory const&, std::istream&, Json::Value*, std::string*)'
main.o:main.cpp:(.text+0x2a1): undefined reference to `Json::Value::toStyledString() const'
main.o:main.cpp:(.text+0x2e8): undefined reference to `Json::Value::operator[](char const*)'

推荐答案

尝试一下:

#include <sstream>


...

    std::stringstream httpData;
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData);

...

        Json::Value jsonData;
        Json::CharReaderBuilder jsonReader;
        std::string errs;
        if (Json::parseFromStream(jsonReader, httpData, &jsonData, &errs))
...

            std::cout << "HTTP data was:\n" << httpData.str() << std::endl;

完整代码:

#define CURL_STATICLIB
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <sstream>

#include "curl.h"
#include "json.h"

namespace
{
    std::size_t callback(
            const char* in,
            std::size_t size,
            std::size_t num,
            char* out)
    {
        std::string data(in, (std::size_t) size * num);
        *((std::stringstream*) out) << data;
        return size * num;        
    }
}

int main()
{
    const std::string url("http://date.jsontest.com/");

    CURL* curl = curl_easy_init();

    // Set remote URL.
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    // Don't bother trying IPv6, which would increase DNS resolution time.
    curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    // Don't wait forever, time out after 10 seconds.
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

    // Follow HTTP redirects if necessary.
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    // Response information.
    int httpCode(0);
    // std::unique_ptr<std::string> httpData(new std::string());

    std::stringstream httpData;

    // Hook up data handling function.
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);

    // Hook up data container (will be passed as the last parameter to the
    // callback handling function).  Can be any pointer type, since it will
    // internally be passed as a void pointer.
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData);

    // Run our HTTP GET command, capture the HTTP response code, and clean up.
    curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
    curl_easy_cleanup(curl);

    if (httpCode == 200)
    {
        std::cout << "\nGot successful response from " << url << std::endl;

        // Response looks good - done using Curl now.  Try to parse the results
        // and print them out.
        Json::Value jsonData;
        Json::CharReaderBuilder jsonReader;
        std::string errs;

        if (Json::parseFromStream(jsonReader, httpData, &jsonData, &errs))
            // jsonReader.parse(httpData, jsonData))
        {
            std::cout << "Successfully parsed JSON data" << std::endl;
            std::cout << "\nJSON data received:" << std::endl;
            std::cout << jsonData.toStyledString() << std::endl;

            const std::string dateString(jsonData["date"].asString());
            const std::size_t unixTimeMs(
                    jsonData["milliseconds_since_epoch"].asUInt64());
            const std::string timeString(jsonData["time"].asString());

            std::cout << "Natively parsed:" << std::endl;
            std::cout << "\tDate string: " << dateString << std::endl;
            std::cout << "\tUnix timeMs: " << unixTimeMs << std::endl;
            std::cout << "\tTime string: " << timeString << std::endl;
            std::cout << std::endl;
        }
        else
        {
            std::cout << "Could not parse HTTP data as JSON" << std::endl;
            std::cout << "HTTP data was:\n" << httpData.str() << std::endl;
            return 1;
        }
    }
    else
    {
        std::cout << "Couldn't GET from " << url << " - exiting" << std::endl;
        return 1;
    }

    return 0;
}

结果:

Got successful response from http://date.jsontest.com/
Successfully parsed JSON data

JSON data received:
{
    "date" : "11-14-2017",
    "milliseconds_since_epoch" : 1510699420942,
    "time" : "10:43:40 PM"
}

Natively parsed:
    Date string: 11-14-2017
    Unix timeMs: 1510699420942
    Time string: 10:43:40 PM

这篇关于使用JSON解析问题警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆