用于 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;