-1

As you can see the array mapped by the below string contains a JSON object whose attributes might have values that are again arrays of JSON objects.
What is a simple way to parse this to an array of JSON objects (for all elements in the hierarchy)?

[{"address_components":
    [{"long_name":"China","short_name":"CN","types" ["country","political"]}],
    "formatted_address":"Volksrepublik China",
    "geometry":{
        "bounds":{
            "$":{"b":18.1535216,"d":53.56097399999999},
            "fa":{"b":73.49941360000003,"d":134.77280989999997}
        },
        "location": {"jb":35.86166,"kb":104.19539699999996},
        "location_type":"APPROXIMATE",
        "viewport":{
            "$":{"b":18.1535216,"d":53.56097399999999},
            "fa":{"b":73.49941360000003,"d":134.77280989999997}
        }
    },
    "types":["country","political"]
}]
Lester
  • 1,830
  • 1
  • 27
  • 44
  • Just deserialize it. In whatever language you are using.... ? – GolezTrol Jul 19 '13 at 07:48
  • What do you want to use to parse it? JS, .NET, Phyton... – OzrenTkalcecKrznaric Jul 19 '13 at 07:48
  • oops, I want to use Java. – Lester Jul 19 '13 at 08:07
  • There are a number of JSON parsing libraries already written for Java. See this question: http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java. There are [others](http://json-lib.sourceforge.net/) [out](https://github.com/ralfstx/minimal-json) [there](http://jackson.codehaus.org/), too. It's just a matter of picking one. – jpmc26 Jul 19 '13 at 08:15

2 Answers2

3

The JavaScript function JSON.parse(str) should return an array with the nested components in place. If you are using other languages like Java, Python or Ruby, check the respective references or look it up elsewhere on SO.

icedwater
  • 4,701
  • 3
  • 35
  • 50
Joe Beuckman
  • 2,264
  • 2
  • 24
  • 63
2

This is basically a json stirng. Check more about it here:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

The JSON data interchange format is easily supported in Java. These are three popular third party Java libraries to process JSON data, which are Jackson, Google Gson and JSON.simple.

This link may help you.

It contains a Java JSON Tutorial for these libraries.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
  • Thanks, I was completely brain stuck: I only thought of JSON-Objects and didn't realize any JSON-parser would also successfully handle a JSON-Array. :) – Lester Jul 19 '13 at 08:23