简体   繁体   中英

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.

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM