首页 > 解决方案 > 在 Ruby on Rails 中反转 JSON 对象顺序

问题描述

我有来自 Twitter 搜索 API 的推文的 JSON 结果,按 created_at 降序排列。反转此 JSON 顺序以便它们按升序排列的最简单方法是什么?

{
   "data" : [
      {
         "created_at" : "2021-04-04T14:21:20.000Z",
         "id" : "1378714305974132736",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "text" : "But my Mom? my Mom? I will give that woman the world, and heaven will fall before I let her down. HEAVEN WILL FALL!!!! .\n\nI pray for everyone that is sick. May God heal you and provide resources  \n\n \"HAPPY SURVIVAL ANNIVERSARY TO ME\"\nWHAT GOD CANNOT DO, DOES NOT EXIST!!!!",
         "author_id" : "1036922707961626624"
      },
      {
         "text" : "the sickness, and I feel bad about it sometimes.  But I know God will soon open financial doors for me. I'm grateful to my brothers, sisters, cousins, aunties, uncles, in-laws, friends, for being there for me in those dark times. Their Love kept me pushing.",
         "author_id" : "1036922707961626624",
         "id" : "1378714303755325444",
         "created_at" : "2021-04-04T14:21:19.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850"
      },
      {
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:19.000Z",
         "id" : "1378714301637263360",
         "author_id" : "1036922707961626624",
         "text" : "hurts sometimes. I don too suffer o. I just want to see where to make money and live a soft life. I deserve it, especially since I can no longer do things that are too tedious. And to complete my mother's house. She hasn't completed her house because of the millions she spent on"
      },
      {
         "text" : "So plix, I'm accepting birthday gifts . This thread may be long but it is only a glimpse of what I went through. What I have narrated doesn't even give out one tenth of what I went through between 2017 and 2019, but I am most grateful to God. I am good now but my scars still",
         "author_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:18.000Z",
         "id" : "1378714299590447110",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624"
      },
      {
         "id" : "1378714297463934982",
         "created_at" : "2021-04-04T14:21:18.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "Like my mom said, I was brought back to life on the ressurection day. I came back from the dead with him. He brought me back to my family. He wiped the tears from my mom's eyes. Easter Sundays will forever be special to me and well, I'm taking it as my second birthday,",
         "author_id" : "1036922707961626624"
      },
      {
         "text" : "to 2019, I had so many blood transfusions.  I have like 50 people's blood in me, if not moreâ¥ï¸. I remember a time I even had partial amnesia. Guyyyy!!! I no dey remember anything again . Why am I posting this today? My Sixth and final surgery was on Easter Sunday, 2019.",
         "author_id" : "1036922707961626624",
         "id" : "1378714295278649346",
         "created_at" : "2021-04-04T14:21:17.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850"
      },
      {
         "author_id" : "1036922707961626624",
         "text" : "In bits, but I could eat. My doctors said I was a miracle. They said the hospital would never forget me. They had never seen anything like what they saw in my tummy that day and they still couldn't believe I survived but I did. I survived. 6 times, I survived. And Omo, from 2017",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:17.000Z",
         "id" : "1378714293382873090"
      },
      {
         "id" : "1378711697721061376",
         "created_at" : "2021-04-04T14:10:58.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "Long story short, I got better. I was healed. Everything had ended. I was free.  I was alive. The only thing I felt was excruciating pain which was normal. The fistula was gone, poo only came out of my anus, the bloating was gone, the obstruction was gone. I could eat now.",
         "author_id" : "1036922707961626624"
      },
      {
         "id" : "1378711694353035265",
         "created_at" : "2021-04-04T14:10:57.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "through the night. The doctors said it was risky to let me sleep that first night.  When it was dawn and I was still alive, my doctors were happy. Then the maintenance process began, to make sure my outer wounds healed and that the fistula didn't return and it did NOT return.",
         "author_id" : "1036922707961626624"
      },
      {
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "id" : "1378711690729160704",
         "created_at" : "2021-04-04T14:10:56.000Z",
         "author_id" : "1036922707961626624",
         "text" : "I did, my brother came to me,  held my hand real tight and kissed my forehead. I could feel how fast his heart was beating, he had been so scared. My mom couldn't contain her joy. She just kept smiling at me. My mom and sister had to take turns that night to keep me awake all"
      }
   ],
   "meta" : {
      "result_count" : 10,
      "oldest_id" : "1378711690729160704",
      "newest_id" : "1378714305974132736",
      "next_token" : "-"
   }
}

我知道我可以遍历这些 JSON 对象,将 created_at 转换为 datetime,将每个对象放入数组中,然后按 created_at 列对数组进行排序。但这似乎非常低效,尤其是在 JSON 很大的情况下。那么最好的解决方案是什么?

标签: ruby-on-railsjsonruby

解决方案


解决了。我可以.reverse在数据上使用


推荐阅读