SanJo
SanJo

Reputation: 11

How to parse JSON array with no object name

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

EDIT: 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
      ]
   }
]

Upvotes: 0

Views: 4259

Answers (2)

Stephen C
Stephen C

Reputation: 719004

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

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

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


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.


In response to this comment:

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.

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.

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47729

It's an array containing one element. That element is an object. The object (dictionary) contains about 20 name/value pairs.

Upvotes: 1

Related Questions