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

标签 c++ json xcode parsing

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

相关文章:

javascript - 在使用 UIWebView 时将 Javascript 作为本地资源包含在 Xcode 4 中

c# - 编译 C++ .lib 文件使它们成为 C# 环境的 DLL

C++ 在同一个程序中使用不同的代码来处理 vector 和列表的排序

使用 xml2js 数组元素名将 json 转换为 xml

php - 使用 PHP 解析 JSON 文件

有效负载的 ASP.net MVC HTTP 请求错误

ios - XCode 12 和 IOS 14 中 UITableView 的问题

c++ - 更改智能指针对象并在之后访问它

c++ - 关于图形(GUI)的使用

objective-c - 得到 PN 但是申请 :application didReceiveRemoteNotification: not triggered when app icon is selected