If you are using an API which gives the response in JSON format then in your Android Studio project you'll need to generate java classes to prase the response.
It's little harder if you are a development starter. For this task, there are a few online tools that can help you generate Java classes out of your JSON response.
For Example, if your response from the API you use is like
Then the java classes for this looks like
There is an open-source project that can help you with this called JSONtoPOJO
Firstly, get the sample JSON response from your API on your browser window then paste it to the box in JsontoPojo.
You'll need to provide your app's package name, the library you use to prase JSON ( Like Gson etc... ) You'll have options there.
Then you can download the zip file with all the java classes. Paste them in your project and that's all.
It's an awesome utility that saves a little time of yours.
If you have any kind of issues feel free to ask me down in the comments section. Looking forward to hearing from you all.
It's little harder if you are a development starter. For this task, there are a few online tools that can help you generate Java classes out of your JSON response.
For Example, if your response from the API you use is like
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York"
}
}
Then the java classes for this looks like
class Address {
JSONObject mInternalJSONObject;
Address (JSONObject json){
mInternalJSONObject = json;
}
String getStreetAddress () {
return mInternalJSONObject.getString("streetAddress");
}
String getCity (){
return mInternalJSONObject.getString("city");
}
}
class Person {
JSONObject mInternalJSONObject;
There is an open-source project that can help you with this called JSONtoPOJO
Firstly, get the sample JSON response from your API on your browser window then paste it to the box in JsontoPojo.
You'll need to provide your app's package name, the library you use to prase JSON ( Like Gson etc... ) You'll have options there.
Then you can download the zip file with all the java classes. Paste them in your project and that's all.
It's an awesome utility that saves a little time of yours.
If you have any kind of issues feel free to ask me down in the comments section. Looking forward to hearing from you all.
0 Comments