如何在c++中存储一个字符串集合作为键,json作为值

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

本文关键字:字符串 集合 json 一个 c++ 存储      更新时间:2023-10-16

我正试图将键值对的集合存储在cpp中,其中key将是一个字符串,值也是如此——在我的情况下,是一个表示对象的JSON字符串。

然后我需要使用Key1访问这个json对象例如

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

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

我想访问

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

当我输入"name1"时。我可以做些什么来将这种数据存储在cpp中。

看起来应该将Value数据放入一个结构中:

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

现在有一个使用字符串和值结构的std::map

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

插入类似于:

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

获取值:

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