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 convert JSON to Struct with embeded subject #1310

Closed
mahuichao opened this issue Oct 22, 2018 · 3 comments
Closed

How to convert JSON to Struct with embeded subject #1310

mahuichao opened this issue Oct 22, 2018 · 3 comments
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@mahuichao
Copy link

I am very glad to see this library and thanks for ur effort .I will make my problem short:
if I have a json like:
{
"SentenceStressScore": 54,
"WordsStressScore": 54,
"consonant_score": 54,
"words_info": [{
"score": 50,
"word": "HOW"
}, {
"score": 50,
"word": "ARE"
}, {
"score": 50,
"word": "YOU"
}]
}

and I want to use struct to get it,here is my code:
`#include
#include
#include
#include "json.hpp"

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

struct words{
float score;
float word;
};

struct gop{
float SentenceStressScore;
float WordsStressScore;
float consonant_score;
vector words_info;
};

void to_json(json&j, gop& g){
    j=json{{"SentenceStressScore",g.SentenceStressScore},{"WordsStressScore",g.WordsStressScore},{"consonant_score",g.consonant_score},{"words_info",g.words_info}}
}
void from_json(const json& j, gop& g) {
    j.at("SentenceStressScore").get_to(g.SentenceStressScore);
    j.at("WordsStressScore").get_to(g.WordsStressScore);
    j.at("consonant_score").get_to(g.consonant_score);
    j.at("words_info").get_to(g.words_info);
}

int main(){
string s2="{ "SentenceStressScore": 54,"WordsStressScore": 54,"consonant_score": 54, "words_info": [{"score": 50, "word": "HOW"}, {"score": 50, "word": "ARE" }, { "score": 50, "word": "YO"}]}";

    auto j2=json::parse(s2);
    gop g=j2;
    cout<<g.SentenceStressScore<<endl;

}`
But this is wrong ,I can't find how to do it? help,pz,I appreciate.

@nlohmann
Copy link
Owner

What exactly is the problem? What is the error message or unexpected behavior?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Oct 22, 2018
@mahuichao
Copy link
Author

main.cpp: In function ‘void to_json(json&, gop&)’:
main.cpp:35:94: error: no matching function for call to ‘nlohmann::basic_json<>::basic_json()’
{"pauseScore",g.pauseScore},{"sentence_score",g.sentence_score},{"words_info",g.words_info}}

@mahuichao
Copy link
Author

refer to the issue #716 ,solved,thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

2 participants