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

Create a json object for every cycle #1937

Closed
giorgioloi opened this issue Feb 11, 2020 · 2 comments
Closed

Create a json object for every cycle #1937

giorgioloi opened this issue Feb 11, 2020 · 2 comments

Comments

@giorgioloi
Copy link

Hello! I'm trying to create a new json object for every loop in the while, (every while loop is a processed frame with a different data). I want to create a Json like this:
frame.json

[
"0":{
    "age": 28.1982421875,
    "male prob": 0.9951171875,
    "id": 0 }
},
"1":{
    "age": 31.1982421875,
    "male prob": 0.8851171888,
    "id": 0 }
]

or
frame.json

[{
    "age": 28.1982421875,
    "male prob": 0.9951171875,
    "id": 0 },
{
    "age": 31.1982421875,
    "male prob": 0.8851171888,
    "id": 0 }
]

main.cpp

#include "json.hpp"
#include <iostream>
#include <fstream>
using json = nlohmann::json;

int main(){
json j;
int framecnt = 0;
while (true)
    {
        //...frame-processing
        
        j.push_back(json::object_t::value_type("id", idx));
        j.push_back(json::object_t::value_type("male prob", r.maleProb));
        j.push_back(json::object_t::value_type("age", r.age));

        std::ofstream o("frame.json");
        o << std::setw(4) << j << std::endl;
       framecnt++;
    }
}

With the push_back I have always one object, what can I use for an output like that?
Sorry I'm not very familiar with c++ and this library. Thanks!

@dota17
Copy link
Contributor

dota17 commented Feb 12, 2020

for the second:

[{
    "age": 28.1982421875,
    "male prob": 0.9951171875,
    "id": 0 },
{
    "age": 31.1982421875,
    "male prob": 0.8851171888,
    "id": 0 }
]

the code should be

#include "json.hpp"
#include <iostream>
#include <fstream>
using json = nlohmann::json;

int main(){
json j;
int framecnt = 0;
while (true)
    {
        //...frame-processing
        json tmp;
       tmp["id"] = idx;
       tmp["male prob"] = r.maleProb;
       tmp["age"] = r.age;

        j.push_back(tmp);

        std::ofstream o("frame.json");
        o << std::setw(4) << j << std::endl;
       framecnt++;
    }
}

@giorgioloi
Copy link
Author

Thank you so much! It works :)

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

2 participants