首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为用于编辑C++的.json数据指定编号

为用于编辑C++的.json数据指定编号
EN

Stack Overflow用户
提问于 2019-02-25 18:33:25
回答 1查看 419关注 0票数 0

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

我有这样的代码:

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
{
  "(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变量。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-25 20:04:46

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

代码语言:javascript
复制
{
    "airports": [{
            "address": "Horley, Gatwick RH6 0NP, UK",
            "city": "London",
            "shortVersion": "(LGW)"
        },
        {
            "address": "Marupe, LV-1053",
            "city": "Riga",
            "shortVersion": "(RIX)"
        }
    ]
}

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

编辑:索引是根据机场在json数组中的顺序隐含的。"London“将是索引0,"Riga”1等等...

访问机场的代码将取决于您正在使用的json库。其伪代码如下所示:

代码语言:javascript
复制
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库:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54864200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档