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

Impossible to do an array of composite objects #691

Closed
mati-araujo opened this issue Aug 10, 2017 · 6 comments
Closed

Impossible to do an array of composite objects #691

mati-araujo opened this issue Aug 10, 2017 · 6 comments
Assignees
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mati-araujo
Copy link

mati-araujo commented Aug 10, 2017

I need to create a JSon array like this for example:

json j;
j.push_back(" { { "objectId" : 0 } , {"transfer_data" : [ {"transfer_id" : 0 } , {"transfer_time" : 57 } ]  } } ");
j.push_back(" { { "objectId" : 1 } , {"transfer_data" : [ {"transfer_id" : 1 } , {"transfer_time" : 49 } ]  } } ");

But the push_back function only acept initializer list that contains only two elements.

@nlohmann
Copy link
Owner

How should the JSON value look like in the end? I am not sure why you use quotes around the value you pass to push_back.

@mati-araujo
Copy link
Author

mati-araujo commented Aug 10, 2017

It was just an example. I want to do somthing like this:

json o;
json j;
o["objectId"] = 0 ;
o["transferData"]["transfer_id"] = 0;
o["transferData"]["transfer_time"] = 57;
j.push_back(o.dump());
o["objectId"] = 1 ;
o["transferData"]["transfer_id"] = 1;
o["transferData"]["transfer_time"] = 49;
j.push_back(o.dump());

the JSON should look like this:

  [ { 
        "objectId" : 0, 
        "transfer_data" : [  
            {
              "transfer_id" : 0 , 
              "transfer_time" : 57 
            } 
       ]  
    } ,
    { 
        "objectId" : 1, 
        "transfer_data" : [  
            {
              "transfer_id" : 1 , 
              "transfer_time" : 49 
            } 
       ]  
    } 
  ]

@nlohmann
Copy link
Owner

You need to pass a json to push_back - so instead of j.push_back(o.dump()), call j.push_back(o).

This should produce the desired JSON:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main() {

    json o;
    json j;
    o["objectId"] = 0 ;
    o["transferData"]["transfer_id"] = 0;
    o["transferData"]["transfer_time"] = 57;
    j.push_back(o);
    o["objectId"] = 1 ;
    o["transferData"]["transfer_id"] = 1;
    o["transferData"]["transfer_time"] = 49;
    j.push_back(o);

    std::cout << std::setw(2) << j << std::endl;
}

@nlohmann
Copy link
Owner

You can also create the array elements like this:

json j;

j.push_back(
{
    {"objectId", 0},
    {"transferData",
        {
            {"transfer_id", 0},
            {"transfer_time", 57}
        }
    }
});

j.push_back(
{
    {"objectId", 1},
    {"transferData",
        {
            {"transfer_id", 1},
            {"transfer_time", 49}
        }
    }
});

or even

json j = {
    {
        {"objectId", 0},
        {"transferData",
            {
                {"transfer_id", 0},
                {"transfer_time", 57}
            }
        }
    },
    {
        {"objectId", 1},
        {"transferData",
            {
                {"transfer_id", 1},
                {"transfer_time", 49}
            }
        }
    }
};

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Aug 10, 2017
@mati-araujo
Copy link
Author

Oh my bad! Thank you!

@nlohmann
Copy link
Owner

You're welcome!

@nlohmann nlohmann self-assigned this Aug 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants