Anil Gautam
Anil Gautam

Reputation: 31

how to store a collection of string as key and json as value in c++

I am trying to store a collection of key-value pair in cpp, where key will be a string, as will the value - in my case, a JSON string representing an object.

Then I need to access this json object using Key1 For Example

Key1 = name1 Value1 = {name:"Anil Gautam","age":25}

Key2 = name2 Value2 = **strong text** = {name:"Sharan Gupta","age":26}

I want to access

{name:"Anil Gautam","age":25} 

when I input "name1". What Can I possible do to store this kind of data in cpp.

Upvotes: 1

Views: 1878

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57749

Looks like you should put the Value data into a structure:

struct Value
{
  std::string name;
  unsigned int age;
};

Now to have a std::map using a string and the value structure:

typedef std::map<std::string, Value> Map_Type;

Insertion is like:

Value v("Anil Gautam", 25);
Map_Type entries;
entries["name1"] = v;

To fetch the value:

Value v2;
v2 = entries["name1"];

Upvotes: 1

Related Questions