如何用 Java 解析这个 JSON 数组?我很困惑,因为没有对象。谢谢!
编辑:我是个白痴!我应该阅读文档...这可能就是它的用途...
[
{
"id":"63565",
"name":"Buca di Beppo",
"user":null,
"phone":"(408)377-7722",
"address":"1875 S Bascom Ave Campbell, California, United States",
"gps_lat":"37.28967000",
"gps_long":"-121.93179700",
"monhh":"",
"tuehh":"",
"wedhh":"",
"thuhh":"",
"frihh":"",
"sathh":"",
"sunhh":"",
"monhrs":"",
"tuehrs":"",
"wedhrs":"",
"thuhrs":"",
"frihrs":"",
"sathrs":"",
"sunhrs":"",
"monspecials":"",
"tuespecials":"",
"wedspecials":"",
"thuspecials":"",
"frispecials":"",
"satspecials":"",
"sunspecials":"",
"description":"",
"source":"ripper",
"worldsbarsname":"BucadiBeppo31",
"url":"www.bucadebeppo.com",
"maybeDupe":"no",
"coupontext":"",
"couponimage":"0",
"distance":"1.00317",
"images":[
0
]
}
]
最佳答案
这是完全有效的 JSON。它是一个包含一个对象的数组。
在 JSON 中,数组和对象没有名称。只有对象的属性才有名称。
这一切都由 http://json.org 处的 JSON 语法图清楚地描述了。 。 (FWIW,该网站有多种语言的翻译......)
<小时/>你如何解析它?有很多用于解析 JSON 的库。其中许多都是从上面的网站链接的。我建议您使用其中之一,而不是编写自己的解析代码。
<小时/>回应此评论:
OTOH, writing your own parser is a reasonable project, and a good exercise for both learning JSON and learning Java (or whatever language). A reasonable parser can be written in about 500 lines of text.
在我看来(我曾经编写过许多解析器),为某种语言编写解析器是一种非常低效的获得语言语法理解的方法。根据您实现解析器的方式(以及语言语法规范的性质),您很容易得到不正确的理解。
更好的方法是阅读语言的语法规范,OP 现在已经完成了这一点,并且为了实现解析器,您必须这样做。
编写解析器可能是一次很好的学习练习,但这实际上是一次编写解析器的学习练习。即使如此,您也需要选择合适的实现方法以及合适的解析语言。
关于java - 如何解析没有对象名称的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6964387/