如何在c ++中访问序列化JSON代码中的子类?

时间:2014-10-25 02:45:43

标签: c++ json serialization deserialization

我正在使用http://miloyip.github.io/rapidjson/md_doc_tutorial.html下载此序列化代码http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155,我正在尝试获取/访问密钥“lasttradeprice”但我的JSON不是最好的,我无法弄清楚如何访问子类,特别是几个层次。

如何在c ++中访问JSON子类? 这可能是101级JSON问题。

错误:

testGetprice2.o: rapidjson/include/rapidjson/document.h:830: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator> = rapidjson::GenericValue<rapidjson::UTF8<> >]: Assertion `false' failed.
Aborted

代码:

#include "rapidjson/include/rapidjson/document.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <curl/curl.h>
#include <unistd.h>
#include <unordered_map>
#include <string>

using namespace rapidjson;

struct myData
{
        std::fstream *file;
        std::string *str;
};

size_t write_data(void *ptr, size_t size, size_t nmemb, myData *data)
{
        size_t numBytes = size * nmemb;

        if (data->file)
                data->file->write((char*)ptr, numBytes);

        if (data->str)
                *data->str += std::string((char*)ptr, numBytes);

        return numBytes;
}

//function to get coin data and perform analysis
int getData()
{
        int count = 0;

    //begin non terminating loop
        while(true)
        {
                count++;
                CURL *curl = curl_easy_init();
                if (curl)
                {
                        curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");

                        std::fstream file("/home/coinz/cryptsy/myfile.txt", std::ios_base::out | std::ios_base::ate);
                        std::string json;

                        myData data;
                        data.file = &file;
                        data.str = &json;

                        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
                        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);

                        /* Perform the request, res will get the return code */
                        CURLcode res = curl_easy_perform(curl);

                        /* Check for errors */
                        if (res != CURLE_OK)
                        {
                                std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
                        }
                        else
                        {
                                file << std::endl;

                                //begin deserialization
                                Document document;
                                document.Parse(json.c_str());
                                assert(document.HasMember("return":"markets"));
                                assert(document["lasttradeprice"].IsString());
                                std::cout << "The Last Traded Price is = " << document["return"].GetString() << std::endl;
                        }

                        /* always cleanup */
                        curl_easy_cleanup(curl);
                }

                //timer for URL request.  *ADUJST ME AS DESIRED*
                usleep(10000000);
        }

        return 0;
}

//Le Main
int main(void)
{
    getData();
}

1 个答案:

答案 0 :(得分:0)

您需要使用for循环迭代它:

for (MemberIterator m = document["a"].MemberBegin(); m != document["a"].MemberEnd(); ++m) {
    std::cout << m.name << " " << (m.IsNumber()?m.GetNumber():m.GetString()) << endl;
}