0

I am new to Boost and Json. It should be very simple, but I can't find the answer.

How do I read a value that is vector of strings in C++ using Boost.

Content of Json file for example :

{  
"keyword1": ["string1", "string2"],  
"keyword2": ["string3", "string4"] 
}

Finally I would like to have vector for each keyword:

vector<string> keyword1;
vector<string> keyword2;
Maria B
  • 13
  • 4
  • Possible duplicate of [this question](https://stackoverflow.com/questions/15206705/reading-json-file-with-boost). In a nutshell, you should try first and then present your work to the community for any help. – Soumya Kanti Jan 14 '19 at 05:06

2 Answers2

0

Your best bet inside boost is to use something as the one in this question: Reading JSON with Boost property_tree

If you need to get it simple, I highly recommend that you use https://github.com/nlohmann/json and use json::parse API.

Germán Diago
  • 7,473
  • 1
  • 36
  • 59
0

Thank you for the help.

This code worked for me :

boost::property_tree::ptree pt;
boost::property_tree::read_json("test.json", pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("entry_name"))
{
     std::cout << v.second.data() << std::endl;
}

The content of 'test.json' file:

{
    "entry_name": ["string1", "string2", "string3"]
}

Output of the code :

string1
string2
string3

I will just add that I tried different parsing of multiple string values , including :

std::vector<std::string> vec = pt.get<std::vector<std::string>> ("entry_name");

That was wrong.

I didn't want to add new classes/ libs like 'rapidJson' or 'nlohmann' , only boost library.

Maria B
  • 13
  • 4