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

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

nlohmann-json: add something to json object

问题

我有这个JSON文件:

  1. {
  2. "user1" : {
  3. "sell" : [
  4. { "id":1001, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
  5. { "id":1002, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
  6. ],
  7. "buy" : [
  8. { "id":2001, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
  9. { "id":2002, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
  10. ]
  11. },
  12. "user2" : {
  13. "sell" : [
  14. { "id":403049, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
  15. { "id":434432, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
  16. ],
  17. "buy" : [
  18. { "id":32144, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
  19. { "id":5244, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
  20. ]
  21. }
  22. }

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

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

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

英文:

I've got this json file:

  1. {
  2. "user1" : {
  3. "sell" : [
  4. { "id":1001, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
  5. { "id":1002, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
  6. ],
  7. "buy" : [
  8. { "id":2001, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
  9. { "id":2002, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
  10. ]
  11. },
  12. "user2" : {
  13. "sell" : [
  14. { "id":403049, "name" : "some name1", "price" : 100, "available" : 10, "category" : "xxx"},
  15. { "id":434432, "name" : "some name2", "price" : 200, "available" : 10, "category" : "xyz"}
  16. ],
  17. "buy" : [
  18. { "id":32144, "name" : "some name4", "price" : 100, "available" : 10, "category" : "jkdhsajdh"},
  19. { "id":5244, "name" : "some name7", "price" : 200, "available" : 10, "category" : "dasdasfjk"}
  20. ]
  21. }
  22. }

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

  1. std::ifstream f(argv[1]);
  2. 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 对象中,然后再将其推送到数组中。

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

  1. json newEntry;
  2. newEntry["id"] = 2003;
  3. newEntry["name"] = "另一个名称";
  4. ...
  5. 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):

  1. json newEntry;
  2. newEntry["id"] = 2003;
  3. newEntry["name"] = "another name";
  4. ...
  5. 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:

确定