【问题标题】:Assigning numbers to .json data for editing C++为 .json 数据分配数字以编辑 C++
【发布时间】:2019-02-25 10:33:25
【问题描述】:

我需要将用户输入的数字分配给 .json 数据的显示部分,以便我可以在后面的函数中编辑数据。

我有这个代码:

int choice;
int numeration = 1;
for (auto& check : airports.items())   //outputs airport city and airport code numbered from 1
{
    std::cout << numeration << ". " << airports[check.key()]["city"] << " " << airports[check.key()]["shortVersion"] << std::endl;
    numeration++;
}

std::cout << "Your choice";    //user inputs the number of the displayed airport
std::cin >> choice;

这是 .json 文件。

{
  "(LGW)": {
     "address": "Horley, Gatwick RH6 0NP, UK",
     "city": "London",
     "shortVersion": "(LGW)"
    },
  "(RIX)": {
     "address: "Marupe, LV-1053",
     "city": "Riga",
     "shortVersion": "(RIX)"
    }
}

如何将用户输入的数字分配给显示的机场,以便程序稍后仅编辑所选数据中的变量或单独删除整个组,如 (LGW) 或 (RIX)?例如,用户输入 1(即为 (LGW)),然后他可以编辑 (LGW) 下的 cityaddressshortVersion 变量。

【问题讨论】:

    标签: c++ json database editing


    【解决方案1】:

    我会将机场存储在一个数组中,如下所示:

    {
        "airports": [{
                "address": "Horley, Gatwick RH6 0NP, UK",
                "city": "London",
                "shortVersion": "(LGW)"
            },
            {
                "address": "Marupe, LV-1053",
                "city": "Riga",
                "shortVersion": "(RIX)"
            }
        ]
    }
    

    json 中的数组是有序的,参见:json.org。然后,您将使用用户输入的索引来访问所需的机场。

    编辑:索引是基于 json 数组中机场的顺序隐含的。 “伦敦”的索引为 0,“里加”的索引为 1,等等...

    访问机场的代码取决于您使用的 json 库。其伪代码类似于:

    int user_selection = 1; // Whatever number the user picked...
    JsonArray airports = json["airports"];
    JsonObject airport = airports[user_selection]; // Index the array based on user's input
    airport["city"] = "Amsterdam"; // Change city from Riga to Amsterdam
    

    Edit2:使用 nlohmann json 库:

    #include "json.hpp"
    
    #include <iostream>
    #include <string>
    
    std::string GenerateJson(const std::string& city, const std::string& address, const std::string& iata_code)
    {
      json j;
      json j_array = json::array();
      json j_object = json::object();
    
      j_object["city"] = city;
      j_object["address"] = address;
      j_object["shortVersion"] = iata_code;
      j_array.emplace_back(j_object);
      j["airports"] = j_array;
    
      return j.dump(4);
    }
    
    int main()
    {
      auto json = R"(
      {
          "airports": [{
                  "address": "Horley, Gatwick RH6 0NP, UK",
                  "city": "London",
                  "shortVersion": "LGW"
              },
              {
                  "address": "Marupe, LV-1053",
                  "city": "Riga",
                  "shortVersion": "RIX"
              }
          ]
      }
    )"_json;
    
      int index = 1;
      auto& airports = json["airports"];
      for (const auto& airport : airports)
      {
        std::cout << index << ") " << airport["city"].get<std::string>() << " " << airport["shortVersion"].get<std::string>() << std::endl;
        ++index;
      }
    
      int choice = 0;
      std::cout << "Your choice:" << std::endl;
      std::cin >> choice;
    
      choice -= 1;
    
      std::string iata_code;
      std::cout << "Change IATA airport code:" << std::endl;
      std::cin >> iata_code;
    
      auto& airport = airports[choice];
      airport["shortVersion"] = iata_code;
    
      std::cout << json.dump(4) << std::endl;
    
      int any;
      std::cout << "Press any key to exit..." << std::endl;
      std::cin >> any;
    }
    

    【讨论】:

    • 如何使用您的代码将输入的索引分配给其中一个机场,以便用户可以对选定的机场进行操作?
    • 我正在使用 nlohmann 的 json.hpp 库。 github.com/nlohmann/json 这是我得到它的链接,它位于单个包含中。你能用这个库写代码吗?我对 json 真的很陌生,才几天,我真的不知道这些库是如何区分的。谢谢!
    • 谢谢,这就是我要找的!希望我能弄清楚如何在我的代码中实现这一点!
    • 您是否知道如何将我的 json 更改为与您的相似?我似乎在谷歌或官方网站上的任何地方都找不到如何创建像你这样的数组。我是用用户输入创建的,代码是airports["airports"][inputShortVersion]["shortVersion"] = inputShortVersion; airports["airports"][inputShortVersion]["city"] = inputCity; airports["airports"][inputShortVersion]["address"] = inputAddress;
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2019-02-11
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多