Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to parse vector<struct> format #2157

Closed
1 of 3 tasks
FantasyJXF opened this issue Jun 1, 2020 · 1 comment
Closed
1 of 3 tasks

how to parse vector<struct> format #2157

FantasyJXF opened this issue Jun 1, 2020 · 1 comment

Comments

@FantasyJXF
Copy link

I'd like to parse the json fromat like this

#include "json.hpp"

using json = nlohmann::json;
struct Image
{
    std::string url;
    int width;
    int height;
    Image(){
        url = "";
        width = -1;
        height = -1;
    };
    Image(std::string url,int width, int height):
        url(url),width(width),height(height){}
};

namespace ns {
    // a simple struct to model a person
    struct session {
        std::vector<Image> ImageUrls;
        std::string msg;
        int code;
        struct returnBody {
            int ComicId;
        }body;
        struct bboxes
        {
            std::string cords;
        }box;
    };
    void to_json(json& j, const session& s);
    void from_json(const json& j, session& s);
}

void ns::to_json(json& j, const ns::session& s) {
    j = json{
        {"ImageUrls", {json::parse(s.ImageUrls)}},
        {"msg", s.msg},
        {"code", s.code},
        {"returnBody", {
            {"ComicId", s.body.ComicId}}
        },
        {"result", {
            {"boxes", s.box.cords}}
        }
    };

}

void ns::from_json(const json& j, ns::session& s) {
    j.at("ImageUrls").get_to(s.ImageUrls);
    j.at("msg").get_to(s.msg);
    j.at("code").get_to(s.code);
    j.at("returnBody").at("ComicId").get_to(s.body.ComicId);
    j.at("result").at("boxes").get_to(s.box.cords);
}

But my to_json and from_json part runs into error. Would U please tell me how to realize this part?
Mostly vector<struct> part.
Thanks

  • Compiler: /c++/4.2.1
  • Operating system: MAC OS

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch
@FantasyJXF
Copy link
Author

FantasyJXF commented Jun 1, 2020

Sorry to bother, I checked many issues, and worked it out.

The compelete source code main.cpp is as follows:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "json.hpp"

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

namespace ns {
    struct Image
    {
        std::string url;
        int width;
        int height;
        Image(){
            url = "";
            width = -1;
            height = -1;
        };
        Image(std::string url,int width, int height):
                url(url),width(width),height(height){}
    };
    void to_json(json& j, const Image& s);
    void from_json(const json& j, Image& s);

    // a simple struct to model a person
    struct session {
        std::vector<Image> ImageUrls;
        std::string msg;
        int code;
        struct returnBody {
            int ComicId;
        }body;
        struct bboxes
        {
            std::string cords;
        }box;
    };
    void to_json(json& j, const session& s);
    void from_json(const json& j, session& s);
} // ns

void ns::to_json(json& j, const ns::Image& s) {
    j = json{
            {"url", s.url},
            {"width", s.width},
            {"height", s.height}
        };
}

void ns::from_json(const json& j, ns::Image& s) {
    j.at("url").get_to(s.url);
    j.at("width").get_to(s.width);
    j.at("height").get_to(s.height);
}

void ns::to_json(json& j, const ns::session& s) {
    j = json{
        {"ImageUrls", {s.ImageUrls}},
        {"msg", s.msg},
        {"code", s.code},
        {"returnBody", {
            {"ComicId", s.body.ComicId}}
        },
        {"result", {
            {"boxes", s.box.cords}}
        }
    };
}

void ns::from_json(const json& j, ns::session& s) {
    j.at("ImageUrls").get_to(s.ImageUrls);
    j.at("msg").get_to(s.msg);
    j.at("code").get_to(s.code);
    j.at("returnBody").at("ComicId").get_to(s.body.ComicId);
    j.at("result").at("boxes").get_to(s.box.cords);
}

int main()
{
    ns::session Sess;
    Sess = ns::session({{{"https",0,1}, {"htpp",0,1}}, "Successful", 200, 0, "1234,2345"});
    json j = Sess;
    std::cout << std::setw(4) << j << "\n\n";

    return 0;
}

The CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.14)
project(json_parse)

set(CMAKE_BUILD_TYPE "debug")
add_executable(json_parse main.cpp)
set_property(TARGET json_parse PROPERTY CXX_STANDARD 11)

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

No branches or pull requests

1 participant