Skip to content

how to parse vector<struct> format #2157

Description

@FantasyJXF

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions