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"
}
MikO
  • 18,243
  • 12
  • 77
  • 109
Reeebuuk
  • 1,363
  • 2
  • 21
  • 42

2 Answers2

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • 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 '13 at 20:40
  • Is there any performance difference between yours and MikOs solution? – Reeebuuk May 16 '13 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 '13 at 20:52
  • Great and those JsonObjects can been also put into the gson class to get an object. – rekire Feb 05 '15 at 10:04
  • Can you add the imports? – ADTC Mar 15 '18 at 05:23
  • `JsonParser` is deprecated. "No need to instantiate this class, use the static methods instead." – slhck Jul 07 '21 at 11:49
  • Wondering if there is more modern way to do this. – Zhiwei Oct 28 '21 at 16:24
  • There might be a fancier way, but you can do the above in the same same way using the static method, just like the deprecation warning suggests! So replace those first two lines with: JsonObject rootObj = JsonParser.parseString(json).getAsJsonObject(); – nameless Feb 28 '22 at 21:06
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...

Community
  • 1
  • 1
MikO
  • 18,243
  • 12
  • 77
  • 109
  • 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 '13 at 20:04
  • 1
    Just deserialize to JsonObject instead, in your case it is what you are looking for – eugen May 16 '13 at 20:16
  • Done, made a few nested classed and everything works just fine. Thx to all :-) – Reeebuuk May 16 '13 at 20:20