喷射 JSON 解析嵌套 JSON 数组
Spray JSON parse nested JSON Array
我有一个带有字符串列表字段的案例 class,但我无法从 JSON 解析它。我定义了一个 JSON Reader :
val jsonReader = new JsonReader[PeaceWatcherReport] {
override def read(json: JsValue): PeaceWatcherReport = {
val fields = json.asJsObject.fields;
new PeaceWatcherReport(
fields("id").convertTo[String],
LocalDateTime.parse(fields("date").convertTo[String]),
fields("location").convertTo[String],
JsArray(fields("surroundingCitizens")).convertTo[Seq[String]].toList,
JsArray(fields("heardWords")).convertTo[Seq[String]].toList
);
}
}
我按如下方式使用它:
val report : PeaceWatcherReport = JsonParser(record.value().toString).convertTo[PeaceWatcherReport](PeaceWatcherReport.jsonReader);
我收到以下错误:
Expected String as JsString, but got ["0cb9f193-792b-4b99-a4f3-f7510f10d62b","673229b2-4b
ba-4faf-a0cf-30cc58ae9c6c","65148ea2-5140-401f-99db-7d4c1bf2d636","3f1a6c1c-a527-40a8-a690-9f6db701680c","8624e4dd-2f86-4f2a-ac81-d18b
5d7601d3"]
我做错了什么?
提前致谢
事实上我不需要使用 JsonArray
因为它已经是一个 :
val jsonReader = new JsonReader[PeaceWatcherReport] {
override def read(json: JsValue): PeaceWatcherReport = {
val fields = json.asJsObject.fields;
new PeaceWatcherReport(
fields("id").convertTo[String],
LocalDateTime.parse(fields("date").convertTo[String]),
fields("location").convertTo[String],
fields("surroundingCitizens").convertTo[Seq[String]].toList,
fields("heardWords").convertTo[Seq[String]].toList
);
}
}
我有一个带有字符串列表字段的案例 class,但我无法从 JSON 解析它。我定义了一个 JSON Reader :
val jsonReader = new JsonReader[PeaceWatcherReport] {
override def read(json: JsValue): PeaceWatcherReport = {
val fields = json.asJsObject.fields;
new PeaceWatcherReport(
fields("id").convertTo[String],
LocalDateTime.parse(fields("date").convertTo[String]),
fields("location").convertTo[String],
JsArray(fields("surroundingCitizens")).convertTo[Seq[String]].toList,
JsArray(fields("heardWords")).convertTo[Seq[String]].toList
);
}
}
我按如下方式使用它:
val report : PeaceWatcherReport = JsonParser(record.value().toString).convertTo[PeaceWatcherReport](PeaceWatcherReport.jsonReader);
我收到以下错误:
Expected String as JsString, but got ["0cb9f193-792b-4b99-a4f3-f7510f10d62b","673229b2-4b ba-4faf-a0cf-30cc58ae9c6c","65148ea2-5140-401f-99db-7d4c1bf2d636","3f1a6c1c-a527-40a8-a690-9f6db701680c","8624e4dd-2f86-4f2a-ac81-d18b 5d7601d3"]
我做错了什么?
提前致谢
事实上我不需要使用 JsonArray
因为它已经是一个 :
val jsonReader = new JsonReader[PeaceWatcherReport] {
override def read(json: JsValue): PeaceWatcherReport = {
val fields = json.asJsObject.fields;
new PeaceWatcherReport(
fields("id").convertTo[String],
LocalDateTime.parse(fields("date").convertTo[String]),
fields("location").convertTo[String],
fields("surroundingCitizens").convertTo[Seq[String]].toList,
fields("heardWords").convertTo[Seq[String]].toList
);
}
}