nlohmann-json: 向 JSON 对象添加内容

huangapple go评论58阅读模式
英文:

nlohmann-json: add something to json object

问题

我有这个JSON文件:

{
     "user1" : {
         "sell" : [
             { "id":1001, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
             { "id":1002, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
         ],
         "buy" : [
             { "id":2001, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
             { "id":2002, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
         ]
     },
     "user2" : {
         "sell" : [
             { "id":403049, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
             { "id":434432, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
         ],
         "buy" : [
             { "id":32144, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
             { "id":5244, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
         ]
     }
 }

我不知道如何向data["user1"]["buy"]中添加一些项目:

std::ifstream f(argv[1]);
json data = json::parse(f);

那么我该如何向data["user1"]["buy"]中添加一些内容?

英文:

I've got this json file:

{
     "user1" : {
         "sell" : [
             { "id":1001, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
             { "id":1002, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
         ],
         "buy" : [
             { "id":2001, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
             { "id":2002, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
         ]
     },
     "user2" : {
         "sell" : [
             { "id":403049, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
             { "id":434432, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
         ],
         "buy" : [
             { "id":32144, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
             { "id":5244, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
         ]
     }
 }

and I have no idea how to add some items to let's say data["user1"]["buy"]

std::ifstream f(argv[1]);
json data = json::parse(f);

so how can I add some stuff to data["user1"]["buy"]?

答案1

得分: 2

文档1指出,默认情况下,JSON 数组使用 std::vector 表示。因此,您可以简单地使用 push_back

当然,您首先需要将数据打包到另一个 JSON 对象中,然后再将其推送到数组中。

类似于以下方式(未经测试):

json newEntry;
newEntry["id"] = 2003;
newEntry["name"] = "另一个名称";
...

data["user1"]["buy"].push_back(newEntry);
英文:

The documentation says that JSON arrays are represented using std::vector by default. Therefore, you can simply use push_back.

Of course, you'll first have to pack your data into another JSON object before you push it into the array.

Something like this (untested):

json newEntry;
newEntry["id"] = 2003;
newEntry["name"] = "another name";
...

data["user1"]["buy"].push_back(newEntry);

huangapple
  • 本文由 发表于 2023年2月6日 04:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定