tags:

views:

45

answers:

4

So I have a JSONObject (or String..) that looks like this:

    {"locations":[{"GeocodeResponse":{"result":{"formatted_address":"Tchibanga (TCH), Gabon","address_component":[{"long_name":"Tchibanga","type":
["airport","establishment","transit_station"],"short_name":"Tchibanga"},{"long_name":"Mougoutsi","type":
["administrative_area_level_2","political"],"short_name":"Mougoutsi"},{"long_name":"Nyanga","type":
["administrative_area_level_1","political"],"short_name":"Nyanga"},{"long_name":"Gabon","type":
["country","political"],"short_name":"GA"}],"type":["airport","establishment","transit_station"],"geometry":
{"viewport":{"southwest":{"lng":"10.9968524","lat":"-2.8198146"},"northeast":{"lng":"11.0031476","lat":"-2.8135194"}},"location_type":"APPROXIMATE","location":{"lng":"11","lat":"-2.816667"}}},"status":"OK"}}]}

It's however way too much information and I just want to say

{"locations":[{"id":"Tchibanga(TCH)","parentId":"TCH","airport":"Tchibanga","category":"Airport","location":{"longitude":"11","latitude":"-2.816667"},"name":"Nyanga","country":"GA"}]}

How would I go about to parse this properly?

Edit: And no, I'm not interested in getting another library just to parse it.

A: 

It appears that what you want is to simply transform a block of JSON into a different block. Maybe this question will help you out: http://stackoverflow.com/questions/1618038/xslt-equivalent-for-json

eqbridges
A: 

Flexjson lets you include/exclude specific objects within the object graph.

letronje
+1  A: 

Check Jackson library. http://jackson.codehaus.org/

Dattu
+1  A: 

You need to get the JSON library from here (you will have to compile the source and ensure that the classes are on your classpath) and create a JSONObject.

A JSONObject is simply a map containing more maps, arrays and objects. Its quite easy (but cumbersome) to parse because there is so much nesting. Let's take a look at how you would parse out the value of the first long_name. If you look at the JSON source string you will see that the location of long_name is in locations/GeocodeResponse/result/address_component. So you would do something like this:

//create a jsonObject
JSONObject jsonObject = new JSONObject("{ \"locations\" ...<snipped>... ] }");

//run some getters until you get to the address_component
JSONArray locations = (JSONArray)jsonObject.get("locations");
JSONObject location = (JSONObject)locations.get(0); // get the first location
JSONObject geoCodeResponse = (JSONObject)location.get("GeocodeResponse");
JSONObject result = (JSONObject)geoCodeResponse.get("result");
JSONArray addressArray = (JSONArray)result.get("address_component");

//print out the long_name from the address
JSONObject address = (JSONObject)addressArray.get(0);
String longName = (String)address.get("long_name");
System.out.println(longName); //prints Tchibanga

However, I would recommend using JsonPath in order to make your life a lot easier.

Once you have picked out the elements you need, you can then construct the desired output JSONObject.

dogbane
Ah now I see the structure, this solved my issue. Thanks!
Meke
There is an online JSON Formatter (http://jsonformat.com/) which will show you your structure more clearly.
dogbane