Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析json对象后,按时间顺序对其排序_Android_Json_Parsing - Fatal编程技术网

Android 解析json对象后,按时间顺序对其排序

Android 解析json对象后,按时间顺序对其排序,android,json,parsing,Android,Json,Parsing,代码如下: 我想根据时间和/或日期对它们进行排序: 它们已经显示出来了,只是没有按顺序显示。目前,我拥有的是将json对象解析为xml的代码。我只想根据时间和/或日期解析它们。下面的项目是JSON文件的示例,其中有2分钟前、5分钟前等不同的日期和时间 try { // Create the root JSONObject from the JSON string. JSONObject jsonRootObject = new JSONObject(myjsonstring); //Ge

代码如下: 我想根据时间和/或日期对它们进行排序: 它们已经显示出来了,只是没有按顺序显示。目前,我拥有的是将json对象解析为xml的代码。我只想根据时间和/或日期解析它们。下面的项目是JSON文件的示例,其中有2分钟前、5分钟前等不同的日期和时间

try {
 // Create the root JSONObject from the JSON string.
 JSONObject jsonRootObject = new JSONObject(myjsonstring);

 //Get the instance of JSONArray that contains JSONObjects
 JSONArray jsonArray = jsonRootObject.getJSONArray("Postdetails");

 //Iterate the jsonArray and print the info of JSONObjects
 for (int i = 0; i < jsonArray.length(); i++) {
  JSONObject jsonObject = jsonArray.getJSONObject(i);
  //Collections.sort(jsonValues);
  String text = jsonObject.optString("number_of_likes").toString();
  String minute = jsonObject.optString("min_ago").toString();
  //float salary = Float.parseFloat(jsonObject.optString("salary").toString());
  time += minute + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n";
  data += text;

 }

 output.setText(data);
 timeago.setText(time);


} catch (JSONException e) {
 e.printStackTrace();
}

当您提取日期并对其进行排序时,您已经对其进行了解析。不管对你的应用程序是否有意义,不知道你的用例是在第一次解析后缓存结果,可能是通过使用某种DB,但如果你的数据结构像这样简单,那么这可能会有些过分


如果您想遵循缓存策略,可以在其上使用类似SQLite的数据库,以使访问更容易,或者使用其他ORM,或在线提供大量教程的ORM。还有很多,但我发现这两个更容易使用。

当您提取日期进行排序时,您已经对其进行了解析。对于你的应用程序来说,有什么意义呢?不知道你的用例是在第一次解析后缓存结果,可能是通过使用某种DB,但如果你的数据结构像这样简单,那么这可能会有点过分。我如何缓存结果?移动了我对一个问题的答案,请参见下文。
{
  "Postdetails": [
    {
      "min_ago": "2 min ago",
      "number_of_likes": "63",
      "number_of_comments": "92",
      "date": "7 March 2017"
    },
    {
      "min_ago": "5 min ago",
      "number_of_likes": "72",
      "number_of_comments": "123",
      "date": "7 March 2017"
    },
    {
      "min_ago": "9 min ago",
      "number_of_likes": "123",
      "number_of_comments": "9123",
      "date": "10 March 2017"
    },
    {
      "min_ago": "6 min ago",
      "number_of_likes": "133",
      "number_of_comments": "9123",
      "date": "9 March 2017"
    }
  ]
}