I have written a small add-on for inbuilt JSON Lib of Android (org.json.*). Android's in-build JSON library is very permeative and provides just the basic functionalities. There is no Tokenize functionality which can automatically convert a JSON string into its relevant Java Bean class.
Though there are few alternate open source libraries like jackson-json, gson etc. But the inbuilt JSON library of Android is pretty simple to use and with this add-on you'll be able to get the missing flavor of auto-bean conversion.
How to use it ?
Please download the JSONHandler.java and include in your Android project source code. It provides an important method parse() which can convert a JSON string into Java Bean class.
It uses Java re-factoring, so Bean class field name and JSON property name should exactly match. Let's look into how to parse a simple JSON structure-
String jsonString = "{name: 'Jon Smith', id: 1234, " +
"address: {cityName: 'San Diego', pinCode:345, latitude: 23.789, longitude: 78.965}," +
"}";
To parse above JSON structure you need to create a Bean class- Person.java having following properties and their Get/Set methods-
String name;
int id;
Address address;
As you can see Address is again a custom class- Address.java having following properties and their Get/Set methods-
String cityName;
int pinCode;
double latitude;
double longitude;
Once you define above 2 bean classes, you can use following piece of code to parse the JSON string into above bean class-
Person person = (Person)(new JSONHandler().parse(jsonString, Person.class, "com.prasanta"));
In the above code piece, "com.prasanta" is the name of base package under which Bean classes are defined.
Let's take an example where the JSON String itself is an Array-
String jsonString = "messages: [{id: 1, sub: 'Hi1', msg: 'I am here1'}, {id: 2, sub: 'Hi2', msg: 'I am here2'}]";
Above information represents an array of Message objects. Structure of Message.java (with Get/Set methods)-
int id;
String sub;
String msg;
JSONHandler.parse() method accepts only JSON Object and not JSONArray. So, we need to parse this array and pass individual JSON Object to the parse()-
JSONObject object = (JSONObject) new JSONTokener(json_resp.getResponseMessage()).nextValue();
JSONArray messages = object.getJSONArray("messages");
for(int i=0; i LT messages.length(); i++){
JSONObject message = messages.getJSONObject(i);
Message msg = (Message)new JSONHandler().parse(message.toString(), Message.class, "com.prasanta");
}
In the above code piece, "com.prasanta" is the name of base package under which Bean classes are defined.
So, how good is that :-) You can enhance the parsing logic as per your requirement.
Though there are few alternate open source libraries like jackson-json, gson etc. But the inbuilt JSON library of Android is pretty simple to use and with this add-on you'll be able to get the missing flavor of auto-bean conversion.
How to use it ?
Please download the JSONHandler.java and include in your Android project source code. It provides an important method parse() which can convert a JSON string into Java Bean class.
It uses Java re-factoring, so Bean class field name and JSON property name should exactly match. Let's look into how to parse a simple JSON structure-
String jsonString = "{name: 'Jon Smith', id: 1234, " +
"address: {cityName: 'San Diego', pinCode:345, latitude: 23.789, longitude: 78.965}," +
"}";
To parse above JSON structure you need to create a Bean class- Person.java having following properties and their Get/Set methods-
String name;
int id;
Address address;
As you can see Address is again a custom class- Address.java having following properties and their Get/Set methods-
String cityName;
int pinCode;
double latitude;
double longitude;
Once you define above 2 bean classes, you can use following piece of code to parse the JSON string into above bean class-
Person person = (Person)(new JSONHandler().parse(jsonString, Person.class, "com.prasanta"));
In the above code piece, "com.prasanta" is the name of base package under which Bean classes are defined.
Let's take an example where the JSON String itself is an Array-
String jsonString = "messages: [{id: 1, sub: 'Hi1', msg: 'I am here1'}, {id: 2, sub: 'Hi2', msg: 'I am here2'}]";
Above information represents an array of Message objects. Structure of Message.java (with Get/Set methods)-
int id;
String sub;
String msg;
JSONHandler.parse() method accepts only JSON Object and not JSONArray. So, we need to parse this array and pass individual JSON Object to the parse()-
JSONObject object = (JSONObject) new JSONTokener(json_resp.getResponseMessage()).nextValue();
JSONArray messages = object.getJSONArray("messages");
for(int i=0; i LT
JSONObject message = messages.getJSONObject(i);
Message msg = (Message)new JSONHandler().parse(message.toString(), Message.class, "com.prasanta");
}
In the above code piece, "com.prasanta" is the name of base package under which Bean classes are defined.
So, how good is that :-) You can enhance the parsing logic as per your requirement.
this assume that you know exactly the content of the data you received and it structure
ReplyDeleteHi,
ReplyDeletenot exactly. To avoid dependency on actual JSON data format, it is using reflection technique.
Have not tried this yet, but seems like an elegant solution. I was looking at gson, but was turned away because it uses the toString method for conversion. This solution allows to defying the json conversion in any location.
ReplyDeleteHi We are having problem uploading images through JSON webservices.Do you have any solution for this?
ReplyDeleteHi,
ReplyDeleteThank you this class ! I need to convert for my project a string (Json format) to a bean file, but I don't want to deploy a big library like Jackson. Your class is perfect ~
Unfortunately, when it tried to use it, a strange exception occurred at the second line.
"Value this of type java.lang.string cannot be covert to JSONObject". Do you have any idea ?
I am using this JSONHandler.java to parse json object returned from server into java beans in one of my android application. I have tested my functionality on 3 different devices, it works fine only for 2 and in one device it is not able to parse. Following are the errors it throws after debugging
ReplyDelete1. model not present
-> Type not present exception
This error comes only in samsung Galaxy
Any idea about why it is so and what does it mean?
Regards
Ashish Upadhyay
Thanks a lot prasantha... your jsonhandler class solved my problem... thank you very much
ReplyDeleteRegards
Amit Pateriya