-2

I am working on json currently in android, but now i follow getter and setter methods to parse json. For example. if there are 5 fields in json string, so create 5 getter and setter methods in one class and accordingly parse the string.

But now in future if one more field increases in json string, then again i need to add one more getter and setter method in that class. So my question is can we parse json without getter setter, without any class? so that if json changes in future it can be handled well without adding new getter and setter method. Please let me know the dynamic way to handle json.

Aniket-Shinde
  • 213
  • 3
  • 9
  • you can get your getter and setter easily try [this](http://www.jsonschema2pojo.org/) – Kiran Benny Joseph Mar 13 '17 at 06:26
  • Are you parsing directly or using Gson/Jackson? – theblitz Mar 13 '17 at 06:26
  • I don't want getter setter methods logic. right now I use JSONObject to parse the json field – Aniket-Shinde Mar 13 '17 at 06:32
  • @theblitz Gson also need getter setter methods right? – Aniket-Shinde Mar 13 '17 at 06:38
  • use Jackson....see the answer here: http://stackoverflow.com/questions/6796662/using-gson-to-parse-a-json-with-dynamic-key-and-value-in-android – rafsanahmad007 Mar 13 '17 at 06:45
  • @user3530687 Gson doesn't require getters and setters. – theblitz Mar 13 '17 at 07:20
  • @rafsanahmad007 In my opinion Gson is better for a number of reasons. The two main ones are 1. It has been adopted by Google as the de-facto Json parser. 2. Benchmarks show that Gson is much faster for data that is small to medium size (which is what you usually have in an android app). Jackson is faster for large Json data streams. – theblitz Mar 13 '17 at 07:23

1 Answers1

0

you can use these web site http://www.jsonschema2pojo.org/

for creating a class if in future any field increase just put the json in that it will automatically create a class for you .