gpt4 book ai didi

c++ - 按字母顺序对 JSON 值进行排序 C++

转载 作者:行者123 更新时间:2023-11-30 02:36:13 28 4
gpt4 key购买 nike

如果我的json文件是这样的:

[
{
"friendName": "Ann",
"birthday": "1990-04-19",
"favoriteColor": "Purple",
},
{
"friendName": "Rachel",
"birthday": "1995-7-05",
"favoriteColor": "Pink",
},
{
"friendName": "Max",
"birthday": "1993-10-07",
"favoriteColor": "Purple",
},
{
"friendName": "Bob",
"birthday": "1992-02-20",
"favoriteColor": "Red",
}
]

如何获得最小的女孩的名字? (如按降序对女孩的生日字符串进行排序,然后获取列表中的第一个对象 (1993-10-07) 并打印她们的名字)。

我正在为现代 C++ ( https://github.com/nlohmann/json ) 和 Xcode(版本 6)使用 JSON。

在我的项目中,我不知道我将拥有多少对象。有没有办法通过比较对这些字符串进行排序?

最佳答案

关于 nlohmann/json 的一件好事是它提供STL-like access ,它允许使用 nlohmann::json具有 <algorithm> 中的大部分功能.

特别是, std::min_element() 在容器中查找 min 元素时可能会派上用场...给定自定义比较函数:)

在下面的代码中,我使用了 dob_comp() lambda 作为比较函数,用于按出生日期 (dob) 比较人。因此,最年轻的人是“最短出生日期”的人。

[ run it online (关闭 main.cpp 如果它打开,点击 compile ,然后点击 execute )]

// compile with: g++ --std=c++11 this_file.cpp -o your_executable_name

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>

#include "json.hpp"

int main()
{
// sample json object
nlohmann::json j = nlohmann::json::parse("["
"{"
" \"friendName\": \"Ann\","
" \"birthday\": \"1990-04-19\","
" \"favoriteColor\": \"Purple\""
"},"
"{"
" \"friendName\": \"Rachel\","
" \"birthday\": \"1995-07-05\","
" \"favoriteColor\": \"Pink\""
"},"
"{"
" \"friendName\": \"Max\","
" \"birthday\": \"1993-10-07\","
" \"favoriteColor\": \"Purple\""
"},"
"{"
" \"friendName\": \"Bob\","
" \"birthday\": \"1992-02-20\","
" \"favoriteColor\": \"Red\""
"}"
"]");

// converts a date string to a std::tm structure
// assumes the string is formatted as "YYYY-MM-DD"
const auto str_to_time = [] (std::string str) {
std::tm tm;
// http://stackoverflow.com/a/21021900
//std::stringstream ss(str);
//ss >> std::get_time(&tm, "%Y-%m-%d");
strptime(str.c_str(), "%Y-%m-%d", &tm);
return tm;
};

// simplistic comparison of std::tm structures -- compares only the (year,month,day)
const auto time_comp = [] (const std::tm& t1, const std::tm& t2) {
if (t1.tm_year < t2.tm_year)
{
return true;
}
else if (t1.tm_year > t2.tm_year)
{
return false;
}
else if (t1.tm_mon < t2.tm_mon)
{
return true;
}
else if (t1.tm_mon > t2.tm_mon)
{
return false;
}
else if (t1.tm_mday < t2.tm_mday)
{
return true;
}
else if (t1.tm_mday > t2.tm_mday)
{
return false;
}
else
{
return true;
}
};

// I didn't have time to read too much of the "json.hpp" header
// so I used a quick "decltype()" to find the iterator type
using json_iterator_type = decltype(*j.begin());

// compares the DatesOfBirth (dob) of two persons
const auto dob_comp = [&str_to_time, &time_comp] (const json_iterator_type p1, const json_iterator_type p2) {
std::string dob1 = p1["birthday"];
std::string dob2 = p2["birthday"];

auto ttm1 = str_to_time(dob1);
auto ttm2 = str_to_time(dob2);

return time_comp(ttm1, ttm2);
};

// know your <algorithm>'s :)
const auto youngest = *std::min_element(j.begin(), j.end(), dob_comp);

std::cout << "The youngest person is: " << youngest << std::endl;
}

注意:如果要对元素进行排序,可以使用 std::sort() 像这样:

std::sort(j.begin(), j.end(), dob_comp);

注2:查看jq如果你需要一个处理json文件的工具。

关于c++ - 按字母顺序对 JSON 值进行排序 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046173/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com