按字母顺序排序JSON值
Sorting JSON values alphabetically C++
如果我的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的访问,这允许使用nlohmann::json
与<algorithm>
中的大多数功能。
特别是,std::min_element()
可能在寻找容器中的最小元素时派上用场…给定一个自定义比较函数:)
在下面的代码中,我使用dob_comp()
lambda作为比较函数,根据出生日期(dob)对人员进行比较。因此,最年轻的人就是拥有"最低出生日期"的人。
[在线运行(如果打开,关闭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);
Note2:查看jq如果你需要一个工具来处理json文件
相关文章:
- 二叉排序树无法编译
- 在c++中使用nlohmann从类到json的转换
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- C++json插入数组
- 将结构向量排序为子组
- nlohmann-json将一个数组插入到另一个数组中
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 如何使用curlpp通过POST方法上传文件和json数据
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- JSON转换为nlohmann JSON-lib中的结构数组
- 在C++中使用NaCl如何按值对JSON对象进行排序
- 按字母顺序排序JSON值
- JsonCpp如何对Json::值进行std::排序