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 make an array of objects #2070

Closed
mutable-dan opened this issue Apr 29, 2020 · 2 comments
Closed

how to make an array of objects #2070

mutable-dan opened this issue Apr 29, 2020 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mutable-dan
Copy link

I saw a similar question: #470
but it did not work

expected output

    [
        {
            "date": 1588125626000,
            "dateString": "2020-04-29T02:00:26.000Z",
            "device": "share2",
            "direction": "Flat",
            "sgv": 123,
            "sysTime": "2020-04-27T03:10:18.000Z",
            "trend": 4,
            "type": "sgv",
            "utcOffset": 0
        },
        {
            "date": 1588125927000,
            "dateString": "2020-04-29T02:05:26.000Z",
            "device": "share2",
            "direction": "Flat",
            "sgv": 125,
            "sysTime": "2020-04-27T03:05:18.000Z",
            "trend": 4,
            "type": "sgv",
            "utcOffset": 0
        }
    ]

of the many things I tried:

                    json js;
                    for( auto item : vItem )
                    {
                        string strDT;
                        string strST;
                        common::timeTickToString( item.DT, strDT );
                        common::timeTickToString( item.DT, strST );
                        json jsItem =
                        {
                            {
                                { "sgv",        item.value    },
                                { "date",       item.DT       },
                                { "dateString", strDT.c_str() },
                                { "trend",      item.trend    },
                                { "direction",  "Flat"        },
                                { "device",     "share2"      },
                                { "type",       "sgv"         },
                                { "utcOffset",  0             },
                                { "sysTime",    strST.c_str() }
                            }
                        };
                        js.push_back( jsItem );
                    }

The result I get is:

   [
        {
            "date": 1588115726000,
            "dateString": "2020-04-28T23:15:26+0000",
            "device": "share2",
            "direction": "Flat",
            "sgv": 123,
            "sysTime": "2020-04-28T23:15:26+0000",
            "trend": 4,
            "type": "sgv",
            "utcOffset": 0
        }
    ],
    [
        {
            "date": 1588115427000,
            "dateString": "2020-04-28T23:10:27+0000",
            "device": "share2",
            "direction": "Flat",
            "sgv": 123,
            "sysTime": "2020-04-28T23:10:27+0000",
            "trend": 4,
            "type": "sgv",
            "utcOffset": 0
        }
    ]
]

Any help would be appreciated,
thank you

@nlohmann
Copy link
Owner

There is one set of braces too many. Try this:

                        json jsItem =
                        {
                                { "sgv",        item.value    },
                                { "date",       item.DT       },
                                { "dateString", strDT.c_str() },
                                { "trend",      item.trend    },
                                { "direction",  "Flat"        },
                                { "device",     "share2"      },
                                { "type",       "sgv"         },
                                { "utcOffset",  0             },
                                { "sysTime",    strST.c_str() }
                        };

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 29, 2020
@mutable-dan
Copy link
Author

thank you :)
fixed the problem

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