0

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.

Mantar
  • 2,710
  • 5
  • 23
  • 30
  • 1
    have you looked at using one of the JSON libraries from json.org? – Mark Elliot Sep 28 '10 at 12:51
  • TMI, in what way? Do you want to filter out stuff, rewrite JSON or something else? And yes, you do want another lib, not the proof-of-concept one from org.json. Even if you don't yet know you need it :) – StaxMan Sep 29 '10 at 04:00

4 Answers4

1

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: XSLT equivalent for JSON

Community
  • 1
  • 1
Edward Q. Bridges
  • 16,712
  • 8
  • 35
  • 42
1

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

Dattu
  • 11
  • 1
1

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
  • 266,786
  • 75
  • 396
  • 414
  • There is an online JSON Formatter (http://jsonformat.com/) which will show you your structure more clearly. – dogbane Sep 28 '10 at 13:37
0

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

letronje
  • 9,002
  • 9
  • 45
  • 53