nlohmann/json:如何将一个字符串转换成一个列表并遍历这个列表?

at0kjp5o  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(85)

nlohmann/json:如何将一个字符串转换成一个列表并遍历这个列表?
我在一个非常特殊的情况下,将列表 Package 成字符串。请参见下面的“Body”和“lidar_calibration”键:关联值是嵌入到字符串"[]"中的列表。
cat calibration.json

{
    "Head": {
        "ErrorCode": "0",
        "Message": "Success"
    },
    "Body": {
        "lidar_calibration": "[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"
    }
}

字符串
cat calibration.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <nlohmann/json.hpp>

int main() {
  {
    std::ifstream inp("calibration.json");
    std::string out; while(inp >> out) std::cout << out; std::cout << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::cout << inpJson << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::stringstream str; str << inpJson["Body"]["lidar_calibration"];
    nlohmann::json calibJson = nlohmann::json::parse(str);
    std::cout << calibJson << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::stringstream str; str << inpJson["Body"]["lidar_calibration"];
    nlohmann::json calibJson = nlohmann::json::parse(str);
    for (auto const & calib : calibJson) std::cout << calib << std::endl;
  }
}
$ g++ -I /usr/include/nlohmann -o calibration calibration.cpp 

$ ./calibration 
{"Head":{"ErrorCode":"0","Message":"Success"},"Body":{"lidar_calibration":"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"}}

{"Body":{"lidar_calibration":"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"},"Head":{"ErrorCode":"0","Message":"Success"}}

"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"

"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"

的数据
我如何迭代这个列表?

tkqqtvp1

tkqqtvp11#

只是:

std::string str = inpJson["Body"]["lidar_calibration"];
nlohmann::json calibJson = nlohmann::json::parse(str);

字符串
将其转换为字符串,而不是使用运算符<<序列化到另一个json。

相关问题