【发布时间】:2012-06-28 21:53:55
【问题描述】:
我有一个包含许多元素的 json 文件:
{
"code" : "hfuiew89",
"type" : "location",
"coordinates" : [ { "lat" : 40.9861, "lon" : 29.1046, "index" : 1 },
{ "lat" : 40.9976, "lon" : 29.1153, "index" : 2 },
{ "lat" : 40.9809, "lon" : 29.2194, "index" : 3 }]
}
{
"code" : "klsdsjh",
"type" : "location",
"relatedTags" : [ "kolmha" ],
"coordinates" : [ { "lat" : 40.9808, "lon" : 29.1605, "index" : 1 },
{ "lat" : 40.9965, "lon" : 29.1672, "index" : 2 }]
}
我想用 gson 读取该文件,但我找到的所有示例都只针对一个元素。因此在阅读第一个之后,抛出“预期 EOF”异常。我该如何克服呢?
【问题讨论】:
-
JSON 意味着是一个单一的实体(可以是一个对象,或者一个数组)——而你得到的是多个对象。在这种情况下,您真正想要的是顶层的数组,每个对象都作为其中的一个元素。你会影响这个 JSON 的 生成,还是你被这种格式卡住了?