0

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
      ]
   }
]
1
  • Don't know what you mean about there being no object name, this does in fact represent an array with a single object in it, no need for a name for the object in JSON. Commented Aug 6, 2011 at 2:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.