I am using jsoncpp to write the data into json format like following:
Json::Value event;
Json::Value lep(Json::arrayValue);
event["Lepton"] = lep;
lep.append(Json::Value(1));
lep.append(Json::Value(2));
lep.append(Json::Value(3));
lep.append(Json::Value(4));
event["Lepton"] = lep;
Json::StyledWriter styledWriter;
cout << styledWriter.write(event);
I got the following output:
{
"Lepton" : [
1,
2,
3,
4
]
}
I want to write multiple such blocks into my data files. What I eventually want is following:
[
{
"Lepton" : [
1,
2,
3,
4
]
},
{
"Lepton" : [
1,
2,
3,
4
]
}
]
Currently, I am writing [ and then the json entries followed by a , and finally at the end ]. Also I have to remove the last , in the final data file.
Is there a way to do all this automatically by jsoncpp or by other means?
Thanks
Using the suggestion from #Some prorammer dude in the comments section, I did the following:
Json::Value AllEvents(Json::arrayValue);
for(int entry = 1; entry < 3; ++entry)
{
Json::Value event;
Json::Value lep(Json::arrayValue);
lep.append(Json::Value(1 + entry));
lep.append(Json::Value(2 + entry));
lep.append(Json::Value(3 + entry));
lep.append(Json::Value(4 + entry));
event["Lepton"] = lep;
AllEvents.append(event);
Json::StyledWriter styledWriter;
cout << styledWriter.write(AllEvents);
}
I got the desired output as shown below:
[
{
"Lepton" : [
1,
2,
3,
4
]
},
{
"Lepton" : [
2,
3,
4,
5
]
}
]
Basically, I created a Json array and appended the resulting Json objects into it.
Related
I have a particular case which I am trying to solve with minimal changes if possible.
one of the data is
js["key1"]["subkey2"]["subsubkey3"].push_back({1,2,3,{4,5}});
[ 1,2,3,[[4,5]] ]
Later at some stage I want to insert
{1,2,3,{4,6}}
Then it should become
[ 1,2,3,[[4,5],[4,6]] ]
How can I make this possible without making 1,2,3 value as key?
I did some playing. I didn't get the results you were looking for. Here's my code and results so far.
#include <iostream>
#include <json.hpp>
using namespace std;
using JSON = nlohmann::json;
int main() {
JSON json = JSON::object();
JSON key1JSON = JSON::object();
JSON key2JSON = JSON::object();
JSON key3JSON = JSON::array();
key3JSON.push_back( {1,2,3, {4,5} } );
key3JSON.push_back( {6} );
key2JSON["subsubkey3"] = key3JSON;
key1JSON["subkey2"] = key2JSON;
json["key1"] = key1JSON;
cout << json.dump(2) << endl;
}
Output:
{
"key1": {
"subkey2": {
"subsubkey3": [
[
1,
2,
3,
[
4,
5
]
],
[
6
]
]
}
}
}
You'll see that the first push_back pushed an array inside an array, which is probably one level deeper than you wanted, and the second one added a second array, which is also not what you want.
Which means you're probably going to have to write your own method, especially as you want to also handle uniqueness. I personally never free-format data that way you have in your example. But maybe your method would look something like:
bool contains(const JSON &json, const JSON &value) {
... this looks like fun to write.
}
void appendUnique(JSON &json, const JSON &array) {
for (JSON & thisJson: array) {
if (!contains(json, thisJson)) {
json.push_back(thisJson);
}
}
}
I modified my code like this:
void appendUnique(JSON &json, const JSON & array) {
for (const JSON & thisJSON: array) {
json.push_back(thisJSON);
}
}
...
appendUnique(key3JSON, {1,2,3, {4,5} } );
appendUnique(key3JSON, {6} );
And got this:
{
"key1": {
"subkey2": {
"subsubkey3": [
1,
2,
3,
[
4,
5
],
6
]
}
}
}
I'm not going to write the isUnique method. But I think you may have to take this to conclusion.
I have an existing json structure, and I'm trying to add a 2D array of doubles to a new Value in the json tree. Here's the function showing what I've tried to do, with the commented out option of defining a <vector> instead of a 2D array of doubles (both types give me the same incorrect output):
bool My_class::input_2D_array (Value& object_list, geom_object *obj_f) {
Value v;
// n_verts is the number of "vertices[]" in the for loop below
double vert_list[obj_f->n_verts][3];
//std::vector<std::array<double, 3>> vert_list(obj_f->n_verts);
for(int i=0; i < obj_f->n_verts; i++) {
vert_list[i][0] = obj_f->vertices[i]->x;
vert_list[i][1] = obj_f->vertices[i]->y;
vert_list[i][2] = obj_f->vertices[i]->z;
}
v["vertex_list"] = vert_list;
//v["vertex_list"] = vert_list.data();
object_list.append(v);
return true;
}
This is what the output is:
"object_list" :
{
"vertex_list" : true
}
And this is what I'd like the output to be:
"object_list" :
{
"vertex_list": [
[
0.0,
0.0,
-0.625
],
[
0.4522545635700226,
-0.32857829332351685,
-0.2795122265815735
],
[
-0.17274247109889984,
-0.5316557288169861,
-0.2795124053955078
]
]
}
What am I missing here?
Quoting the QT Docs:
You can even store QList and QMap values in
a variant, so you can easily construct arbitrarily complex data
structures of arbitrary types. This is very powerful and versatile,
but may prove less memory and speed efficient than storing specific
types in standard data structures.
Does anyone know of, or have, an example of doing exactly this?
I'm a long time C++ programmer, but a QT Nube, and the copy of write semantics are giving me fits. Maps and Lists of QVariants data structures seems immutable. Every time I try to modify a tree of values, I just end up modifying a copy.
Got some feedback from my first post that I should add an example. Here goes:
// Input Data:
//
// { "f1" : "field-1",
// "list" : [ 0, 1, 2, 3, 4 ] }
//
// Convert the data, commented above, into a QVariantMap with two
// values:
// "f1" - a string
// "list" - a QVariantList of integers
QVariant vData = ConvertJsonDocument(document);
// Dump
qWarning( VariantToString(vData).toLocal8Bit() );
// Convert vData to QVariantMap
QVariantMap vMap = vData.value<QVariantMap>();
// Get the list of integers as a QVariantList
QVariantList vList = vMap["list"].value<QVariantList>();
// Change the 0 to a 5
vList[0] = 5;
// Dump
qWarning( VariantToString(vData).toLocal8Bit() );
Output from above:
{ "f1" : "field-1", "list" : [ 0, 1, 2, 3, 4 ] }
{ "f1" : "field-1", "list" : [ 0, 1, 2, 3, 4 ] }
DESIRED output from above:
{ "f1" : "field-1", "list" : [ 0, 1, 2, 3, 4 ] }
{ "f1" : "field-1", "list" : [ 5, 1, 2, 3, 4 ] }
I get that I am modifying copies, but for the life of my I can't figure out how NOT to. How do I edit the original source data? (The data in the tree rooted at vData.)
Once you make the desired alterations, you need to go back through the tree and update your variables with the new data.
// Convert vData to QVariantMap
QVariantMap vMap = vData.value<QVariantMap>();
// Get the list of integers as a QVariantList
QVariantList vList = vMap["list"].value<QVariantList>();
// Change the 0 to a 5
vList[0] = 5;
// Change the map using insert, which replaces the value
vMap.insert("list", vList);
// Rebuild the QVariant from the QMap
vData = QVariant::fromValue(vMap);
// Dump
qWarning( VariantToString(vData).toLocal8Bit() );
You can convert the data back from the QVariant and update the source document from there.
As per the tutorial:
Each JSON value is stored in a type called Value. A Document, representing the DOM, contains the root Value of the DOM tree.
If so, it should be possible to make a sub-document from a document.
If my JSON is:
{
"mydict": {
"inner dict": {
"val": 1,
"val2": 2
}
}
}
I'd like to be able to create a Document from the inner dictionary.
(And then follow the instructions in the FAQ for How to insert a document node into another document?)
Given the original document contains:
{
"mydict": {
"inner dict": {
"val": 1,
"val2": 2
}
}
}
You can copy the sub-document:
const char json[] = "{\"mydict\": {\"inner dict\": {\"val\": 1, \"val2\": 2}}}";
Document doc, sub; // Null
doc.Parse(json);
sub.CopyFrom(doc["mydict"], doc.GetAllocator());
You can also swap the sub-document with another:
const char json[] = "{\"mydict\": {\"inner dict\": {\"val\": 1, \"val2\": 2}}}";
Document doc, sub; // Null
doc.Parse(json);
sub.Swap(doc["mydict"]);
With some validation:
const char json[] = "{\"mydict\": {\"inner dict\": {\"val\": 1, \"val2\": 2}}}";
Document doc, sub; // Null
doc.Parse(json);
if (doc.IsObject()) {
auto it = doc.FindMember("mydict");
if (it != doc.MemberEnd() && it->value.IsObject()) {
sub.Swap(it->value);
}
}
Each will result in sub containing:
{
"inner dict": {
"val": 1,
"val2": 2
}
}
With the CopyFrom() approach, doc will contain:
{
"mydict": {
"inner dict": {
"val": 1,
"val2": 2
}
}
}
With Swap():
{
"mydict": null
}
So im trying to create a json Object in c++ dynamically. I want to add a timestamp and then an array with the data included.
So thats what my json String would look like :
{
"timestep": "2160.00",
"vehicles": [
{
"id": "35092_35092_353",
"x": "6.988270",
"y": "50.872139",
"angle": "-20.812787",
"type": "passenger_P_14_1",
"speed": "0.000000",
"pos": "4.600000",
"lane": "4.600000",
"slope": "4.600000"
},
{
"id": "35092_35092_353",
"x": "6.988270",
"y": "50.872139",
"angle": "-20.812787",
"type": "passenger_P_14_1",
"speed": "0.000000",
"pos": "4.600000",
"lane": "4.600000",
"slope": "4.600000"
},
{
"id": "35092_35092_353",
"x": "6.988270",
"y": "50.872139",
"angle": "-20.812787",
"type": "passenger_P_14_1",
"speed": "0.000000",
"pos": "4.600000",
"lane": "4.600000",
"slope": "4.600000"
}
]
}
Im totally new to C++ and im using the Casablanca ( C++ REST SDK) package.
So im having a really hard time producing the code. And i cant find any working solutions. I found this on the wiki
Create a JSON object:
json::value obj;
obj[L"key1"] = json::value::boolean(false);
obj[L"key2"] = json::value::number(44);
obj[L"key3"] = json::value::number(43.6);
obj[L"key4"] = json::value::string(U("str"));
and that works for me. But how do i create an array?
i tried several things but nothing worked. Maybe theres a better package? But as far as i understood its an official micorosft package for json and http.
Help would be really nice!
There are 2 mechanisms. If you are used to std c++ libraries, this should look familiar. Element vector is derived from std::vector.
json::value::element_vector e;
// the first item in the pair is the array index, the second the value
e.push_back(std::make_pair(json::value(0), json::value(false)));
e.push_back(std::make_pair(json::value(1), json::value::string(U("hello"))));
json::value arr(e);
And, if you prefer a cleaner look, and can accept a less efficient compiled result:
json::value arr;
arr[0] = json::value(false);
arr[1] = json::value(U("hello"));
From your message you have tried a bunch of stuff. If you have tried mechanisms like these but they didn't work, give us a sample program that demontrates the failure and we'll have a crack at it.
To get the basic structure in your file above:
json::value vehicles;
vehicles[0] = // 1st vehicle object
vehicles[1] = // 2nd vehicle object
// etc
json::value root;
root[L"timestep"] = json::number(2160.0);
root[L"vehicles"] = vehicles;
Here's how to create an array dynamically using vector. Assume that you have 10 vehicles to add.
std::vector<web::json::value> arrayVehicles;
for(int i = 0; i < 10; i++)
{
web::json::value vehicle;
std::string vehicleID = "id_prefix_" + std::to_string(i);
vehicle["id"] = web::json::value::string(vehicleID);
vehicle["x"] = web::json::value::number(6.988270);
vehicle["y"] = web::json::value::number(50.872139);
arrayVehicles.push_back(vehicle);
}
web::json::value myJSON;
myJSON["vehicles"] = web::json::value::array(arrayVehicles);
You could put it like this:
json::value vehicle1;
vehicle1[L"id"] = json::value::string(L"35092_35092_353");
vehicle1[L"x"] = json::value::number(6.988270);
vehicle1[L"y"] = json::value::number(50.872139);
json::value vehicle2;
vehicle2[L"id"] = json::value::string(L"35092_35092_353");
vehicle2[L"x"] = json::value::number(1.23456);
vehicle2[L"y"] = json::value::number(6.78901);
json::value vehicles;
vehicles[L"timestamp"] = json::value::number(2160);
vehicles[L"vehicles"] = json::value::array({vehicle1, vehicle2});
Here is another method to produce a json array in Casablanca:
int size = 3;
web::json::value yourJson;
yourJson[U("vehicles")] = web::json::value::array(size);
yourJson[U("vehicles")].as_array()[0] = web::json::value(U("some entry"));
yourJson[U("vehicles")].as_array()[1] = web::json::value(U("another entry"));
//...
If you wish to use the array as an answer on a received http_request (in case below it's a http_request request), you are free to use the following snippet of code as an example:
json::value answer;
auto array = answer.array();
for (size_t i = 0; i < GenFile.GetNumberOfCurves(); i++)
{
web::json::value vehicle;
vehicle[L"smth"] = web::json::value::number(WhatEverArray[i].whatever());
array[i] = vehicle;
}
request.reply(status_codes::OK, array);
The following sample json C++ array of strings works for me.
const char * const line_items_items =
"[\
{\
\"commodity_code\": \"44121903\",\
\"description\": \"Miscellaneous goods\",\
\"upc\": \"65100004327\",\
\"quantity\": \"2\",\
\"unit_of_measurement\": \"M62\",\
\"unit_cost\": \"23.09\",\
\"discount_amount\": \"10.03\",\
\"total_amount\": \"50.03\",\
\"tax_amount\": \"10.05\",\
\"extended_amount\": \"76.04\",\
\"debit_or_credit_indicator\": \"credit\",\
\"net_or_gross_indicator\": \"net\"\
},\
{\
\"commodity_code\": \"44121809\",\
\"description\": \"Miscellaneous goods\",\
\"upc\": \"65100007654\",\
\"quantity\": \"4\",\
\"unit_of_measurement\": \"M66\",\
\"unit_cost\": \"35.09\",\
\"discount_amount\": \"5.06\",\
\"total_amount\": \"0.53\",\
\"tax_amount\": \"8.07\",\
\"extended_amount\": \"96.12\",\
\"debit_or_credit_indicator\": \"debit\",\
\"net_or_gross_indicator\": \"gross\"\
}\
]";