按字母顺序排序JSON值

Sorting JSON values alphabetically C++

本文关键字:JSON 排序 顺序      更新时间:2023-10-16

如果我的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文件