如果我的 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) 并打印出她们的名字)。
我正在将 JSON 用于现代 C++ ( https://github.com/nlohmann/json ) 和 Xcode(版本 6)。
在我的项目中,我不知道我将拥有多少对象。有没有办法通过比较对这些字符串进行排序?
最佳答案
关于 nlohmann/json 的一件好事是它提供 STL-like access ,允许使用 nlohmann::json
具有 <algorithm>
中的大部分功能.
特别是, std::min_element()
在寻找容器中的最小元素时可能会派上用场...给定自定义比较函数:)
在下面的代码中,我使用了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/