嵌套数组的 Klaxon 解析
Klaxon Parsing of Nested Arrays
我正在尝试用 Klaxon 解析 this file,一般情况下进展顺利,但我完全没有成功解析 features/[Number]/properties/
的子数组
所以我的想法是获取原始属性字符串并使用 Klaxon 单独解析它,尽管我也没有成功。除此之外,我还采用了许多其他方法。
到目前为止我的代码:
class Haltestelle(val type: String?, val totalFeatures: Int?, val features: Array<Any>?)
fun main(args: Array<String>) { // Main-Routine
val haltejsonurl = URL("http://online-service.kvb-koeln.de/geoserver/OPENDATA/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=ODENDATA%3Ahaltestellenbereiche&outputFormat=application/json")
val haltestringurl = haltejsonurl.readText()
val halteklx = Klaxon().parse<Haltestelle>(haltestringurl)
println(halteklx?.type)
println(halteklx?.totalFeatures)
println(halteklx?.features)
halteklx?.features!!.forEach {
println(it)
}
我知道我正在调用作为 Any 数组的功能,所以输出只是打印我 java.lang.Object@blabla 每次。虽然,使用 Array 也失败了。
真的要花几个小时在这上面,你会怎么做?
新手问候
以下是我在 Kotlin 中执行类似操作的方法。您可以将响应解析为 Klaxon JsonObject,然后访问 "features" 元素以将所有数组对象解析为 JsonObjects 的 JsonArray。这可以在您的示例中迭代并使用 parseFromJsonObject 进行转换:
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.github.aivancioglo.resttest.*
val response : Response = RestTest.get("http://anyurlwithJSONresponse")
val myParser = Parser()
val data : JsonObject = myParser.parse(response.getBody()) as JsonObject
val allFeatures : JsonArray<JsonObject>? = response["features"] as JsonArray<JsonObject>?
for((index,obj) in allFeatures.withIndex()) {
println("Loop Iteration $index on each object")
val yourObj = Klaxon().parseFromJsonObject<Haltestelle>(obj)
}
我正在尝试用 Klaxon 解析 this file,一般情况下进展顺利,但我完全没有成功解析 features/[Number]/properties/
的子数组所以我的想法是获取原始属性字符串并使用 Klaxon 单独解析它,尽管我也没有成功。除此之外,我还采用了许多其他方法。
到目前为止我的代码:
class Haltestelle(val type: String?, val totalFeatures: Int?, val features: Array<Any>?)
fun main(args: Array<String>) { // Main-Routine
val haltejsonurl = URL("http://online-service.kvb-koeln.de/geoserver/OPENDATA/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=ODENDATA%3Ahaltestellenbereiche&outputFormat=application/json")
val haltestringurl = haltejsonurl.readText()
val halteklx = Klaxon().parse<Haltestelle>(haltestringurl)
println(halteklx?.type)
println(halteklx?.totalFeatures)
println(halteklx?.features)
halteklx?.features!!.forEach {
println(it)
}
我知道我正在调用作为 Any 数组的功能,所以输出只是打印我 java.lang.Object@blabla 每次。虽然,使用 Array 也失败了。
真的要花几个小时在这上面,你会怎么做?
新手问候
以下是我在 Kotlin 中执行类似操作的方法。您可以将响应解析为 Klaxon JsonObject,然后访问 "features" 元素以将所有数组对象解析为 JsonObjects 的 JsonArray。这可以在您的示例中迭代并使用 parseFromJsonObject
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.github.aivancioglo.resttest.*
val response : Response = RestTest.get("http://anyurlwithJSONresponse")
val myParser = Parser()
val data : JsonObject = myParser.parse(response.getBody()) as JsonObject
val allFeatures : JsonArray<JsonObject>? = response["features"] as JsonArray<JsonObject>?
for((index,obj) in allFeatures.withIndex()) {
println("Loop Iteration $index on each object")
val yourObj = Klaxon().parseFromJsonObject<Haltestelle>(obj)
}