JSON对象按名称(字符串形式的日期)排序

时间:2019-05-08 18:32:03

标签: java json api

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

这是JSON响应的代码段。如您所见,它从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()调用对象名称。即。 JSONOBJECT.names()并通过SimpleDateFormat转换为日期,但是自然JSONObject.name()不包含嵌套在该对象中的任何字符串。

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

1 个答案:

答案 0 :(得分:0)

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

有许多基于Java的JSON处理库可供选择。有些遵循Java API for JSON Binding (JSON-B)作为JSR 367的一部分定义的标准Jakarta EE API,有些则没有。引用实现就是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 );
  

引号:

     

[StockQuote {时间= 2019-05-03T12:30 |量= 238455 |高= 1185.5200 |低= 1180.6000 |开= 1181.3800 | close = 1185.4500},StockQuote {when = 2019-04-23T10:30 |量= 176972 |高= 1264.5500 |低= 1257.6000 |开= 1259.8450 | close = 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 String类中将日期时间值保留为StockQuote。按字母顺序排序时,您输入的内容恰好是按时间顺序的。

when成员的类型从LocalDateTime类的StockQuote更改为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;