38

I have the following JSON and I'm only interested in getting the elements "status", "lat" and "lng".

Using Gson, is it possible to parse this JSON to get those values without creating the whole classes structure representing the JSON content?

JSON:

{
  "result": {
    "geometry": {
      "location": {
        "lat": 45.80355369999999,
        "lng": 15.9363229
      }
    }
  },
  "status": "OK"
}
4
  • 3
    Your JSON is invalid (you can verify it with jsonlint. The inner geometry property of address_components should be wrapped in an object (using braces {}).
    – Perception
    May 16, 2013 at 19:39
  • I've cut a lot of unnecessary nodes and typo occurred :-)
    – Reeebuuk
    May 16, 2013 at 20:03
  • So, whats the correct form? Can't really propose a quick solution without knowing that.
    – Perception
    May 16, 2013 at 20:06
  • JSON is edited, thanks for the json checker... I would never found that error.
    – Reeebuuk
    May 16, 2013 at 20:18

2 Answers 2

64

You don't need to define any new classes, you can simply use the JSON objects that come with the Gson library. Heres a simple example:

JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonObject locObj = rootObj.getAsJsonObject("result")
    .getAsJsonObject("geometry").getAsJsonObject("location");

String status = rootObj.get("status").getAsString();
String lat = locObj.get("lat").getAsString();
String lng = locObj.get("lng").getAsString();

System.out.printf("Status: %s, Latitude: %s, Longitude: %s\n", status,
        lat, lng);

Plain and simple. If you find yourself repeating the same code over and over, then you can create classes to simplify the mapping and eliminate repetition.

8
  • Good solution without a custom deserializer! Anyway I think this approach is much more difficult to maintain and reuse... do you not think it's better and more elegant the usual parsing using classes?
    – MikO
    May 16, 2013 at 20:40
  • Is there any performance difference between yours and MikOs solution?
    – Reeebuuk
    May 16, 2013 at 20:48
  • 1
    For such a small input data the performance improvement will be negligible. For larger data sets there is some time saved in managing the (extra) mapped POJO. But keep in mind that using one method over the other should be driven by a lot more than any perceived gains in performance (for example, the complexity of the incoming data might make the creation of a mapping class unnecessarily tedious).
    – Perception
    May 16, 2013 at 20:52
  • Great and those JsonObjects can been also put into the gson class to get an object.
    – rekire
    Feb 5, 2015 at 10:04
  • Can you add the imports?
    – ADTC
    Mar 15, 2018 at 5:23
6

It is indeed possible, but you have to create a custom deserializer. See Gson documentation here and Gson API Javadoc here for further info. And also take a look at other reponses of mine here and here... and if you still have doubts, comment.

That said, in my opinion it is much easier for you to parse it creating the correspondent classes, even more taking into account the simplicity of your JSON response... With the usual approach you only have to write some super-simple classes, however, writing a custom deserializer, although is not that complex, it will take you probably longer, and it will be more difficult to adapt if later on you need some data else of your JSON...

Gson has a way of operating that has been designed for developers to use it, not for trying to find workarounds!

Anyway, why do you not want to use classes? If you don't like to have many classes in your project, you can just use nested classes and your project will look cleaner...

3
  • When you say it like that I agree with you. I'll try to generate all the classes and respond here it there will be some problems.
    – Reeebuuk
    May 16, 2013 at 20:04
  • 1
    Just deserialize to JsonObject instead, in your case it is what you are looking for
    – eugen
    May 16, 2013 at 20:16
  • Done, made a few nested classed and everything works just fine. Thx to all :-)
    – Reeebuuk
    May 16, 2013 at 20:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.