【问题标题】:JSON Object sorting by name which is date in string formJSON对象按名称排序,名称为字符串形式的日期
【发布时间】:2019-05-08 18:32:02
【问题描述】:

我是 Java 新手,我目前正在尝试通过其(字符串)名称(实际上是日期)从 API 中整理出 JSON 响应。它给了我日期和时间作为包含信息字符串的 JSONObject。我想知道是否有人曾经按他们的“字符串名称”对 JSONObjects 进行排序并按日期对它们进行排序。

这是 JSON 响应的 sn-p。如您所见,它从 5 月 3 日到 4 月 23 日疯狂跳跃。 5 月 2 日还有另一个对象,为了简单起见,我没有在下面发布。

{
  "2019-05-03 12:30:00": {
    "3. low": "1180.6000",
    "5. volume": "238455",
    "1. open": "1181.3800",
    "2. high": "1185.5200",
    "4. close": "1185.4500"
  },
  "2019-04-23 10:30:00": {
    "3. low": "1257.6000",
    "5. volume": "176972",
    "1. open": "1259.8450",
    "2. high": "1264.5500",
    "4. close": "1264.1100"
  }
}

我尝试通过keys() 调用对象名称。 IE。 JSONOBJECT.names() 并通过SimpleDateFormat 转换为日期,但自然JSONObject.name() 不包含任何嵌套在该对象中的字符串。

我仍然需要检索 3. low: 1180 字符串,将它们转换为浮点数并通过项目的统计测试。

【问题讨论】:

  • 您的示例数组似乎不正确,缺少标签和方括号。

标签: java json api


【解决方案1】:

用于 JSON 绑定的 Java API (JSON-B)

有许多基于 Java 的 JSON 处理库可供选择。有些遵循JSR 367 定义的标准Java API for JSON Binding (JSON-B) API 作为Jakarta EE 的一部分,有些则不遵循。一个是参考实现,Eclipse Yasson

如果在您的项目中使用 Maven,这里是您的 POM 的当前条目。

    <!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
    <dependency>
        <groupId>jakarta.json.bind</groupId>
        <artifactId>jakarta.json.bind-api</artifactId>
        <version>1.0.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.eclipse/yasson -->
    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.3</version>
    </dependency>

正确的数组表示法

虽然我不是 JSON 专家,但您的示例似乎格式不正确。

  • 需要方括号来包裹数组。
  • 每个对象都需要一个标签,比如这里的"quote"。您不正确地使用日期时间值作为标签,而它应该是每个报价对象的另一个成员。

并修复日期时间字符串以符合 ISO 8601,java.time 类中默认使用的标准格式。

[
  {
    "when": "2019-05-03T12:30:00",
    "low": "1180.6000",
    "volume": "238455",
    "open": "1181.3800",
    "high": "1185.5200",
    "close": "1185.4500"
  },
  {
    "when": "2019-04-23T10:30:00",
    "low": "1257.6000",
    "volume": "176972",
    "open": "1259.8450",
    "high": "1264.5500",
    "close": "1264.1100"
  }
]

定义一个类

我建议您将每个股票报价解析为您定义的类形式的 Java 对象。

package work.basil.example;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Objects;
import java.util.StringJoiner;

public class StockQuote implements Comparable<StockQuote>
{
    public LocalDateTime when;
    public Long volume;
    public BigDecimal high, low, open, close;

    // --------|  Constructors  |------------------------

    public StockQuote ()
    {
        // No code needed here. No-arg constructor required for use with JSON-B.
    }

    public StockQuote ( LocalDateTime when , Long volume , BigDecimal low , BigDecimal high , BigDecimal open , BigDecimal close )
    {
        this.when = when;
        this.volume = volume;
        this.high = high;
        this.low = low;
        this.open = open;
        this.close = close;
    }

    // --------|  Object  |------------------------

    @Override
    public boolean equals ( Object o )
    {
        if ( this == o ) return true;
        if ( o == null || getClass() != o.getClass() ) return false;
        StockQuote that = ( StockQuote ) o;
        return Objects.equals( this.when , that.when);
    }

    @Override
    public int hashCode ()
    {
        return Objects.hash( this.when );
    }

    @Override
    public String toString ()
    {
        return new StringJoiner( " | " , StockQuote.class.getSimpleName() + "{ " , " }" )
                .add( "when=" + when )
                .add( "volume=" + volume )
                .add( "high=" + high )
                .add( "low=" + low )
                .add( "open=" + open )
                .add( "close=" + close )
                .toString();
    }

    @Override
    public int compareTo ( StockQuote that )
    {
        return this.when.compareTo( that.when );
    }

}

解析为对象集合

准备 JSON 处理器。

Jsonb jsonb = JsonbBuilder.create();

定义一些输入。

String json = "[\n" +
        "  {\n" +
        "    \"when\": \"2019-05-03T12:30:00\",\n" +
        "    \"low\": \"1180.6000\",\n" +
        "    \"volume\": \"238455\",\n" +
        "    \"open\": \"1181.3800\",\n" +
        "    \"high\": \"1185.5200\",\n" +
        "    \"close\": \"1185.4500\"\n" +
        "  },\n" +
        "  {\n" +
        "    \"when\": \"2019-04-23T10:30:00\",\n" +
        "    \"low\": \"1257.6000\",\n" +
        "    \"volume\": \"176972\",\n" +
        "    \"open\": \"1259.8450\",\n" +
        "    \"high\": \"1264.5500\",\n" +
        "    \"close\": \"1264.1100\"\n" +
        "  }\n" +
        "]\n"
;

解析并收集那些引用对象。

List < StockQuote > quotes = jsonb.fromJson( json , new ArrayList < StockQuote >() {}.getClass().getGenericSuperclass() );

最后,对您的收藏进行排序。

Collections.sort( quotes );

转储到控制台。

System.out.println( "quotes:\n" + quotes );

引用:

[股票报价{ when=2019-05-03T12:30 |体积=238455 |高=1185.5200 |低=1180.6000 |打开=1181.3800 |关闭=1185.4500 }, StockQuote{ 时间=2019-04-23T10:30 |体积=176972 |高=1264.5500 |低=1257.6000 |打开=1259.8450 |关闭=1264.1100 }]

成功。

我们可以容忍您的对象成员标签上的编号。

StockQuote 类的每个成员上,添加@JsonbProperty 注释。请参阅Json Binding Users Guide

@JsonbProperty ( "0. when" )
public LocalDateTime when;
@JsonbProperty ( "5. volume" )
public Long volume;
@JsonbProperty ( "2. high" )
public BigDecimal high;
@JsonbProperty ( "3. low" )
public BigDecimal low;
@JsonbProperty ( "1. open" )
public BigDecimal open;
@JsonbProperty ( "4. close" )
public BigDecimal close;

让我们放回那些编号的标签。

    String json = "    [\n" +
            "      {\n" +
            "        \"0. when\": \"2019-05-03T12:30:00\",\n" +
            "        \"3. low\": \"1180.6000\",\n" +
            "        \"5. volume\": \"238455\",\n" +
            "        \"1. open\": \"1181.3800\",\n" +
            "        \"2. high\": \"1185.5200\",\n" +
            "        \"4. close\": \"1185.4500\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"0. when\": \"2019-04-23T10:30:00\",\n" +
            "        \"3. low\": \"1257.6000\",\n" +
            "        \"5. volume\": \"176972\",\n" +
            "        \"1. open\": \"1259.8450\",\n" +
            "        \"2. high\": \"1264.5500\",\n" +
            "        \"4. close\": \"1264.1100\"\n" +
            "      }\n" +
            "    ]"
    ;

我们现在得到了同样成功的结果。

日期时间字符串

虽然我不推荐它,但您可以在 Java StockQuote 类中将日期时间值保留为 String。按字母顺序排序时,您的输入恰好是按时间顺序排列的。

when 成员的类型从StockQuote 类上的LocalDateTime 更改为String

@JsonbProperty ( "0. when" )
public String when;

撤消我们对输入日期时间字符串的修复。

    String json = "    [\n" +
            "      {\n" +
            "        \"0. when\": \"2019-05-03 12:30:00\",\n" +
            "        \"3. low\": \"1180.6000\",\n" +
            "        \"5. volume\": \"238455\",\n" +
            "        \"1. open\": \"1181.3800\",\n" +
            "        \"2. high\": \"1185.5200\",\n" +
            "        \"4. close\": \"1185.4500\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"0. when\": \"2019-04-23 10:30:00\",\n" +
            "        \"3. low\": \"1257.6000\",\n" +
            "        \"5. volume\": \"176972\",\n" +
            "        \"1. open\": \"1259.8450\",\n" +
            "        \"2. high\": \"1264.5500\",\n" +
            "        \"4. close\": \"1264.1100\"\n" +
            "      }\n" +
            "    ]"
    ;

或者,您可以将when 成员保留为LocalDateTime。添加@JsonbDateFormat注解指定传入数据的格式。

@JsonbProperty ( "0. when" )
@JsonbDateFormat ("uuuu-MM-dd HH:mm:ss")
public LocalDateTime when;

【讨论】:

  • 抱歉回复晚了,非常感谢非常详细的回答。不幸的是,我的 Date 对象只是嵌套在另一个任意对象中,所以我不能像你那样使用字符串标识符来解析它。我创建了一个 java 类并遍历了 json 响应。由于每个键都是一个日期,我使用 SimpleDateFormat() 对其进行格式化,并抓取这些键中的每个字符串并将它们格式化为浮点数,并将日期和股票数据作为对象添加到 java 类中。不管怎样,你的回答很有见地,Basil 教会了我一些东西!
猜你喜欢
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 2012-11-16
  • 2018-08-17
  • 1970-01-01
  • 2012-12-25
相关资源
最近更新 更多