如何解析没有对象名称的JSON数组 [英] How to parse JSON array with no object name

查看:118
本文介绍了如何解析没有对象名称的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何用Java解析这个JSON数组?我很困惑,因为没有对象。谢谢!

How would I parse this JSON array in Java? I'm confused because there is no object. Thanks!

编辑:我是个白痴!我应该阅读文档...这可能是它的用途......

I'm an idiot! I should have read the documentation... that's probably what it's there for...

[
   {
      "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。它是一个包含一个对象的数组。

It is perfectly valid JSON. It is an array containing one object.

在JSON中,数组和对象没有名称。只有对象的属性才有名称。

In JSON, arrays and objects don't have names. Only attributes of objects have names.

这一切都是通过 http://json.org 。 (FWIW,该网站有多种语言的翻译......)

This is all described clearly by the JSON syntax diagrams at http://json.org. (FWIW, the site has translations in a number of languages ...)

你如何解析它?有许多用于解析JSON的库。其中许多是从上面的网站链接。我建议你使用其中一个而不是编写自己的解析代码。

How do you parse it? There are many libraries for parsing JSON. Many of them are linked from the site above. I suggest you use one of those rather than writing your own parsing code.

回复此评论:


OTOH,编写自己的解析器是一个合理的项目,也是学习JSON和学习Java(或任何语言)的良好练习。合理的解析器可以用大约500行文本编写。

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.

在我看来(在我的时间里写了很多解析器),为一种语言编写解析器是非常低效的获得理解语言语法的方法。根据您实现解析器的方式(以及语言语法规范的性质),您可以轻松获得不正确的理解。

In my opinion (having written MANY parsers in my time), writing a parser for a language is a very inefficient way to gain a working understanding the syntax of a language. And depending on how you implement the parser (and the nature of the language syntax specification) you can easily get an incorrect understanding.

更好的方法是阅读OP现在已经完成的语言语法规范,以及为了实现解析器而必须执行的操作。

A better approach is to read the language's syntax specification, which the OP has now done, and which you would have to do in order to implement a parser.

编写解析器可以一个很好的学习练习,但它实际上是编写解析器的学习练习。即便如此,您还需要选择适当的实现方法,并使用适当的语言进行解析。

Writing a parser can be a good learning exercising, but it is really a learning exercise in writing parsers. Even then, you need to pick an appropriate implementation approach, and an appropriate language to be parsed.

这篇关于如何解析没有对象名称的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆