I would like to iterate over each of entries in a json object, but I am getting one incomprehensible error after the other. How to correct the following example?
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
void bla(std::string a) {
std::cout << a << '\n';
}
int main() {
json RecentFiles;
RecentFiles["1"]["Name"] = "test1.txt";
RecentFiles["1"]["Last modified"] = "monday";
RecentFiles["1"]["Score"] = 5.0f;
RecentFiles["2"]["Name"] = "test2.txt";
RecentFiles["2"]["Last modified"] = "tuesday";
RecentFiles["2"]["Score"] = 5.0f;
for (auto it = RecentFiles.begin(); it != RecentFiles.end(); ++it) {
bla("JSON: Recent file = " + it.value()["Name"]);
}
std::cout << RecentFiles; }
Error:
prog.cc: In function 'int main()':
prog.cc:18:31: error: invalid conversion from 'const char*' to 'nlohmann::detail::iter_impl<nlohmann::basic_json<> >::difference_type {aka long int}' [-fpermissive]
std::cout << it["Name"];
^
In file included from prog.cc:2:0:
./nlohmann/json.hpp:4418:15: note: initializing argument 1 of 'nlohmann::detail::iter_impl<BasicJsonType>::reference nlohmann::detail::iter_impl<BasicJsonType>::operator[](nlohmann::detail::iter_impl<BasicJsonType>::difference_type) const [with BasicJsonType = nlohmann::basic_json<>; nlohmann::detail::iter_impl<BasicJsonType>::reference = nlohmann::basic_json<>&; nlohmann::detail::iter_impl<BasicJsonType>::difference_type = long int]'
reference operator[](difference_type n) const
^
The above is done in the sandbox
https://wandbox.org/permlink/LNck7Gktm14bmPy0
This is not actual code that I am using, I just want to see if I can understand how to do the various basic things that I need to do with JSON.
Currently I understand so little, that I do not know if what I am doing is essentially correct but just breaks due to something stupid, or if I am doing something fundamentally wrong.
The nlohmann json library promotes itself as "JSON for modern C++" and aspires to behave "just like an STL container". There is, however, no container in the C++ standard library that is both "vector-like" and "map-like", and that supports both begin/end iterators over values, and begin/end iterators over key/value pairs. So something new is needed.
nlohmann's original solution was to copy jsoncpp's approach, which supports begin/end iterators for json arrays, and adds a distinctly unstandard key() function to the iterator to also support json objects. So you could write
for (auto it = RecentFiles.begin(); it != RecentFiles.end(); ++it)
{
std::cout << it.key() << "\n";
std::cout << (*it)["Name"].get<std::string>() << "\n";
std::cout << (*it)["Last modified"].get<std::string>() << "\n";
}
But being an unstandard way of iterating over key/values, this doesn't have standard library support for range based for loops over key/values.
nlohmann later added the json::items() function that does support iteration over json objects with standard iterators, and which does have standard library support for range based for loops, viz.
int main()
{
json RecentFiles;
RecentFiles["1"]["Name"] = "test1.txt";
RecentFiles["1"]["Last modified"] = "monday";
RecentFiles["1"]["Score"] = 5.0f;
RecentFiles["2"]["Name"] = "test2.txt";
RecentFiles["2"]["Last modified"] = "tuesday";
RecentFiles["2"]["Score"] = 5.0f;
for (const auto& item : RecentFiles.items())
{
std::cout << item.key() << "\n";
for (const auto& val : item.value().items())
{
std::cout << " " << val.key() << ": " << val.value() << "\n";
}
}
std::cout << "\nor\n\n";
for (const auto& item : RecentFiles.items())
{
std::cout << item.key() << "\n";
std::cout << " " << item.value()["Name"].get<std::string>() << "\n";
std::cout << " " << item.value()["Last modified"].get<std::string>() << "\n";
std::cout << " " << item.value()["Score"].get<double>() << "\n";
}
}
Output:
1
Last modified: "monday"
Name: "test1.txt"
Score: 5.0
2
Last modified: "tuesday"
Name: "test2.txt"
Score: 5.0
or
1
test1.txt
monday
5
2
test2.txt
tuesday
5
Related
I am fairly new to boost::property_tree and I am having a little trouble with what should be a simple task.
I have a default xml file of which is to be copied and made unique with parameters passed into via the ptree & modelModifier(...) function below. All I want to do is parse the xml into a ptree, and then modify the name field (amongst others, but lets just start with name) from "default" to whatever name is passed in from the object_name variable, then write that back into the original ptree.
The function pTreeIterator just iterates through each child and displays its contents.
xml
<?xml version='1.0'?>
<sdf version='1.7'>
<model name='default'>
<link name='link'>
<inertial>
<mass>3.14999</mass>
<inertia>
<ixx>2.86712</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>2.86712</iyy>
<iyz>0</iyz>
<izz>0.524998</izz>
</inertia>
<pose>0 0 0 0 -0 0</pose>
</inertial>
</link>
</model>
</sdf>
Code
void ptreeIterator(ptree & pt)
{
using boost::property_tree::ptree;
for (auto&it : pt)
{
std::cout << __FUNCTION__ << std::endl;
std::cout << "------------------------------------------------------" << std::endl;
std::cout << "Iteration output: " << std::endl;
std::cout << "1: pt1: " << it.first << std::endl;
if(pt.get_child_optional(it.first))
{
cout << "pt.get_child_optional(it.first) ---> " << it.first << endl;
ptree pt2 = pt.get_child(it.first);
for (auto&it2 : pt2)
{
std::cout << "\t2: " << "pt2: " << it2.first << " :: " << (std::string)it2.second.data() << std::endl;
if(pt2.get_child_optional(it2.first))
{
ptree pt3 = pt2.get_child(it2.first);
for (auto&it3 : pt3)
{
std::cout << "\t\t3: " << "pt3: " << it3.first << " :: " << (std::string)it3.second.data() << std::endl;
}
}
}
}
}
}
ptree & modelModifier(ptree &model, double px, double py, std::string object_name, uint16_t height)
{
for(auto &it:model){
cout << "it.first = " << it.first << endl;
if(it.first=="model"){
cout << "MODEL TAG" << endl;
model.put_value(object_name);
}
ptreeIterator(model);
}
}
int main(){
ptree ptModel;
const string filenameToInsert = "model.sdf";
std::ifstream ifsModel(filenameToInsert,std::ifstream::binary);
read_xml(ifsModel, ptModel, boost::property_tree::xml_parser::trim_whitespace);
modelModifier(ptModel, 0, 0, "TEST1234567", 30);
return 0;
}
Output
it.first = model
it.second.data()
ptreeIterator
------------------------------------------------------
Iteration output:
1: pt1: model
pt.get_child_optional(it.first) ---> model
2: pt2: <xmlattr> ::
3: pt3: name :: default
Expected Output
it.first = model
it.second.data()
ptreeIterator
------------------------------------------------------
Iteration output:
1: pt1: model
pt.get_child_optional(it.first) ---> model
2: pt2: <xmlattr> ::
3: pt3: name :: TEST1234567
Firstly, your code has UB, since modelModifier doesn't return a value.
The C-style cast in (std::string)it2.second.data() is extremely dangerous as it risks reinterpret_cast-ing unrelated types. There is no reason whatsoever for this kind of blunt casting. Just remove the cast!
Also, ptreeIterator should probably take a ptree const&, not ptree&.
With these fixed, the sample does NOT show the output you claim, instead it prints (Live On Coliru)
it.first = sdf
ptreeIterator
------------------------------------------------------
Iteration output:
1: pt1: sdf
pt.get_child_optional(it.first) ---> sdf
2: pt2: <xmlattr> ::
3: pt3: version :: 1.7
2: pt2: model ::
3: pt3: <xmlattr> ::
3: pt3: link ::
Now even in your question output, you clearly see the difference between the model node and its name attribute, which apparently you want to modify. Just write the code to access that:
it.second.get_child("<xmlattr>.name").put_value(object_name);
This would be correct, assuming that the attribute always exists and instead of ptModel you pass ptModel.get_child("sdf") to modifyModel).
Other Notes: SIMPLIFY!
That said, please simplify the whole thing!
ptree pt2 = pt.get_child(it.first);
Should have been something like
ptree const& pt2 = it.second;
And
the use of get_child_optional only to repeat with get_child is even more wasteful
Good practice is to separate output/query and mutation. So don't call ptreeIterator from inside modelModifier
Also, give functions a good descriptive name (so that you don't have sheepishly explain "The function pTreeIterator just iterates through each child and displays its contents" - just call it displayModel?)
Instead of painstakingly (and flawed) iterating the particular model and printing it in pretty confusing bespoke manner, just use write_xml/write_info/write_json to dump it in a reliable manner.
Listing
Live On Coliru
namespace Model {
void display(ptree const& pt)
{
write_json(std::cout << __FUNCTION__ << "\n---------------\n", pt);
}
void modify(ptree& model, double, double, std::string object_name, uint16_t)
{
for (auto& it : model) {
std::cout << "root = " << it.first << std::endl;
it.second.get_child("model.<xmlattr>.name").put_value(object_name);
}
}
}
Prints
root = sdf
display
---------------
{
"sdf": {
"<xmlattr>": {
"version": "1.7"
},
"model": {
"<xmlattr>": {
"name": "TEST1234567"
},
"link": {
"<xmlattr>": {
"name": "link"
},
"inertial": {
"mass": "3.14999",
"inertia": {
"ixx": "2.86712",
"ixy": "0",
"ixz": "0",
"iyy": "2.86712",
"iyz": "0",
"izz": "0.524998"
},
"pose": "0 0 0 0 -0 0"
}
}
}
}
}
Bonus
In the case that the name attribute might not already exist, the following code would create it on the fly:
void modify(ptree& model, double, double, std::string object_name, uint16_t)
{
ptree value;
value.put_value(object_name);
for (auto& it : model) {
std::cout << "root = " << it.first << std::endl;
it.second.put_child("model.<xmlattr>.name", value);
}
}
I'm new to C++ and I have a vector of doctors.
I add a new doctor with the following code:
void DoctorAdmin::setDoctor(std::string lastname, std::string forename,
Person::Sex sex){
//Create new doctor
Doctor* doc = new Doctor(lastname, forename, sex);
//insert at the end of the vector
doctors.push_back(doc);
}
Then I want to show their information on the console:
void DoctorAdmin::showDoctors(){
cout << "Doctors:" << endl;
cout << "Name" << "\t\t\t" << "Forename" << "\t\t\t" << "Sex" << endl;
for (vector<Doctor*>::iterator i = doctors.begin(); i != doctors.end(); i++){
Doctors* doc = doctors.at(i);
cout << doc->getName() << "\t\t\t" << doc->getForename() << "\t\t\t"
<< doc->getSex() << endl;
}
After doing it like this I get two Errors:
E0304 No instance of overloaded function "std::vector<_Ty, _Alloc>::at [mit _Ty=Doctors *, _Alloc=std::allocator<Doctors *>]" matches the argument list.
// and
C2664 "Doctors *const &std::vector<Doctors *,std::allocator<_Ty>>::at(const unsigned int) const" : cannot convert from Argument "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>" in "const unsigned int"
How do I use the vector iterator correctly to avoid this?
An iterator is not index-like, it is pointer-like.
for (vector<Arzt*>::iterator doc = aerzte.begin(); doc != aerzte.end(); doc++)
{
cout << (*doc)->getName() << "\t\t\t" << (*doc)->getVorname() << "\t\t\t"
<< (*doc)->getGeschlecht() << endl;
}
It seems like you are confused as to when you need to new things too. Most of the time you don't need new
vector<Arzt> aerzte;
void ArztAdmin::anlegenArzt(std::string name, std::string vorname, Person::Geschlecht geschlecht){
// Create new doctor at the end of the vector
aerzte.emplace_back(name, vorname, geschlecht);
}
You can also directly bind references as loop variables
for (Arzt & doc : aerzte)
{
cout << doc.getName() << "\t\t\t" << doc.getVorname() << "\t\t\t"
<< doc.getGeschlecht() << endl;
}
The at function requires an index, but a vector<Arzt*>::iterator is not an index, neither semantically nor technically. An iterator points directly to an element, whereas an index represents the distance between a container's start and the element in a container that allows random element access.
Because an iterator points directly to an element, the at function isn't even necessary in your loop. *i yields the element:
Arzt* doc = *i;
Beginning with C++11, the code for such simple loops can be written in a shorter way using auto:
for (auto i = aerzte.begin(); i != aerzte.end(); i++){
The compiler knows what type i really is because it knows what begin() returns.
Even better, use a range-based loop:
for (auto doc : aerzte){
cout << doc->getName() << "\t\t\t" << doc->getVorname() << "\t\t\t"
<< doc->getGeschlecht() << endl;
}
And while we're at it, don't use dynamic memory allocation when you don't have to. This isn't Java or C#; new is dangerous territory in C++ and should be avoided:
#include <vector>
#include <string>
#include <iostream>
struct Arzt
{
Arzt(std::string const& name, std::string const& vorname) :
name(name),
vorname(vorname)
{
}
std::string name;
std::string vorname;
// Geschlecht omitted for brevity's sake
};
int main()
{
std::vector<Arzt> aerzte;
Arzt doc1("foo", "bar");
Arzt doc2("foo", "bar");
Arzt doc3("foo", "bar");
aerzte.push_back(doc1);
aerzte.push_back(doc2);
aerzte.push_back(doc3);
for (auto const& arzt : aerzte)
{
std::cout << arzt.name << ' ' << arzt.vorname << '\n';
}
}
As you are no longer iterating over pointers but over larger objects, const& should be used in the for loop.
I have yahoo finance json file from which I want to isolate Date,Close and volume from the quote list and save it in the same order with a comma separtion in a single text file. This is my json script.
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse( YahooJson, root );
if(not parsingSuccessful)
{
// Report failures and their locations
// in the document.
std::cout<<"Failed to parse JSON"<<std::endl
<<reader.getFormatedErrorMessages()
<<std::endl;
return 1;
}else{
std::cout<<"\nSucess parsing json\n"<<std::endl;
std::cout << root<< std::endl;
std::cout <<"No of Days = "<< root["query"]["count"].asInt() << std::endl;
//below for loop returns an error
for (auto itr : root["query"]["result"]["quote"]) {
std::string val = itr.asString();
}
}
I was able to succed in fetching the json values and print root["query"]["count"].asInt() but when I go to the list values(quote) I dont know how to iterate through quote (query->result->quote) to get Date,close and volume values?
EDIT
Also tried this method
const Json::Value& quotes = root["query"]["results"]["quote"];
for (int i = 0; i < quotes.size(); i++){
std::cout << " Date: " << quotes[i]["Date"].asString();
std::cout << " Close: " << quotes[i]["Close"].asFloat();
std::cout << " Volume: " << quotes[i]["Volume"].asFloat();
std::cout << std::endl;
}
It works only when output was Date. For close and volume output it exits with a runtime error message and also this error
what() type is not convertible to string
You haven't specified which JSON library you are using, and I don't know the Yahoo finance data well enough to know the exact field names, but if you are using the JsonCpp library, which has documentation here, and you are asking about how to iterate over a JSON array, then one way to do it using iterators would look something like this
const Json::Value quote = root["query"]["results"]["quote"];
for (Json::ValueConstIterator itr = quote.begin(); itr != quote.end(); ++itr)
{
const Json::Value date = (*itr)["Date"];
const Json::Value close = (*itr)["Close"];
const Json::Value volume = (*itr)["Volume"];
std::cout << "Date: " << date.asString() << std::endl;
std::cout << "Close: " << close.asString() << std::endl;
std::cout << "Volume: " << volume.asString() << std::endl;
}
I am trying to create an array with the content of a vector
using bsoncxx::builder::stream;
auto doc = document{};
doc << "foo"
<< open_array;
for (auto i : v){
doc << open_document << "bar" << i << close_document ;
}
doc << close_array;
I get the following error report:
error: no match for ‘operator<<’ (operand types are ‘bsoncxx::v0::builder::stream::document’ and ‘const bsoncxx::v0::builder::stream::open_document_type’)
doc << open_document << "bar" << i << close_document ;
Any idea on how to do it?
The C++11 bson driver is actually hiding a bit more complexity under the stream style api than it at first appears.
The problem you're encountering is that there is type information encoded into the expression on each successive '<<', so from your original example you'll need to:
auto sz = 100;
// Note that we capture the return value of open_array
auto arr = doc << "foo" << open_array;
for (int32_t i=0; i < sz; i++)
arr << i;
arr << close_array;
This is because open_array creates a nested type that can accept values without keys, unlike doc, which needs key value pairs.
If you want something more inline, you can use the inline Callable facility as in:
auto sz = 100;
builder::stream::document doc;
doc << "foo" << open_array <<
[&](array_context<> arr){
for (int32_t i = 0; i < sz; i++)
arr << i;
} // Note that we don't invoke the lambda
<< close_array;
The callable facility works with things that take:
single_context - write one value in an array, or as the value part of a key value pair
key_context<> - write any number of key value pairs
array_context<> - write any number of values
See src/bsoncxx/test/bson_builder.cpp for more examples
I have the same problem with the streaming builder.
using bsoncxx::builder::stream;
auto doc = document{};
doc << "foo"
<< open_array
<< open_document
<< "bar1" << 1
<< "bar2" << 5
<< close_document
<< close_array;
The above works, however if you want to do the following it doesn't work
doc << "foo"
<< open_array;
for (size_t i=0; i < sz; i++)
doc << i;
doc << close_array;
I get the below error. The problem is that this behavior making using the stream builder is practically useless. Perhaps its a bug or the 10gen guys are not finished with the this part of the API.
doc << closerror: no match for ‘operator<<’ (operand types are ‘bsoncxx::v0::builder::stream::document’ and ‘const bsoncxx::v0::builder::stream::open_document_type’) doc << open_document << "bar" << i << close_document ;e_array;
I'm trying to use boost json with property trees to decode a json message. I'm only interested about checking whether "mykey" is in the message and, if that is the case, get the corresponding values.
I'm a little lost in boost documentation, and I was trying to see what the actual code would be to parse a message such as the one below.
{
// some values
"mykey": [
{
"var1": "value1_str",
"var2" : "value2"
}
]
// some other values
}
I don't know about Boost ptree for JSON. I've tried it but it seemed... very clunky.
Here's a simple JSON parser based on the RFC, made in Spirit: https://github.com/sehe/spirit-v2-json/tree/q21356666
You could use it for your use case like test.cpp
#include <vector>
#include "json.hpp"
struct X {
static X from_json(JSON::Value const& v);
std::string var1;
double var2;
};
int main()
{
auto doc = as_object(JSON::parse(
"{\n"
" // some values\n"
" \"mykey\": [\n"
" {\n"
" \"var1\": \"value1_str\",\n"
" \"var2\" : 3.14\n"
" }\n"
" ]\n"
" // some other values\n"
"}\n"
));
if (doc.has_key("mykey"))
{
X data = X::from_json(doc["mykey"]);
std::cout << "X.var1: " << data.var1 << "\n";
std::cout << "X.var2: " << data.var2 << "\n";
}
std::cout << "doc: " << doc << "\n";
std::cout << "doc[\"mykey\"]: " << doc["mykey"] << "\n";
}
X X::from_json(JSON::Value const& v)
{
X result;
auto& o = as_object(as_array(v)[0]);
result.var1 = as_string(o["var1"]);
result.var2 = as_double(o["var2"]);
return result;
}
Output:
X.var1: value1_str
X.var2: 3.14
doc: {"mykey":[{"var1":"value1_str","var2":3.14}]}
doc["mykey"]: [{"var1":"value1_str","var2":3.14}]
There are other json libraries around. I suggest you select one to suit your needs.