Skip to content

How can I only parse the first level and get the value as string? #1175

Description

@kgbook

I use the latest release version 3.1.2.

I want to parse the JSON data:

"{"json":"{"error":true,"message":"404 not found","code":0,"nonce":"0.8054632351656703","timestamp":1532525070907}", "signature":"Q5s1tDJVXWcNXp"}"

And My code:

main.cpp

#include <iostream>
#include "main.h"

using json = nlohmann::json;
using namespace std;
using namespace tp;

int main() {
    stCfgStr cfgStr;
    json in = "{\"json\":\"{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\":1532525070907}\", \"signature\":\"Q5s1tDJVXWcNXp\"}"_json;

    try {
        decode(in, cfgStr);
        cout <<"json: "<<cfgStr.json <<endl;
        cout <<"signature: " <<cfgStr.signature <<endl;
    }
    catch (json::parse_error &e){
        cout <<"msg: "<<e.what()<<"\n"
              <<"exception id: "<<e.id<<"\n"
              <<"byte position of error: "<<e.byte<<endl;
    }

    return 0;
}

main.h

#ifndef NLOHMANNJSON_SAMPLE_MAIN_H
#define NLOHMANNJSON_SAMPLE_MAIN_H

#include <iostream>
#include <nlohmann/json.hpp>
#include <cstdint>

using namespace std;
using nlohmann::json;

namespace tp {
    struct stCfgStr {
        string json;
        string signature;
    };
}

namespace tp{
    void encode(const stCfgStr &cfg, nlohmann::json &j){
        j = nlohmann::json{{"json", cfg.json}, {"signature", cfg.signature}};
    }

    void decode(const nlohmann::json &j, stCfgStr &cfg){
        cfg.json = j.at("json").get<string>();
        cfg.signature = j.at("signature").get<string>();
    }
}

#endif //NLOHMANNJSON_SAMPLE_MAIN_H

Then I got a exception named json.exception.parse_error, the detail information is

parse error at 12: syntax error - invalid literal; last read: '"{"e'; expected '}'

I just want to decode the value of json object with std::string type, and then decode again.

Note that:

"{"json":"{"error":true,"message":"404 not found","code":0,"nonce":"0.8054632351656703","timestamp":1532525070907}", "signature":"Q5s1tDJVXWcNXp"}"
"{"json":{"error":true,"message":"404 not found","code":0,"nonce":"0.8054632351656703","timestamp":1532525070907}, "signature":"Q5s1tDJVXWcNXp="}"

There are not the same!
As for the first one, the value of json embrace with double quotes ", and the other is not!

So I have to decode the value of json as a string, not object.

I used RapidJson library but failed with the same issue, parseErrorCode:kParseErrorObjectMissCommaOrCurlyBracket .

Is it possible to decode those json data I describe above with the nlomann JSON library?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs more infothe author of the issue needs to provide more details

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions