I'm using gson to map an JSON on Java object. I have JSON that looks similar to example below
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "We have the Munchies.",
"name": "wehavethemunchies",
"posts": 10662,
"url": "http://wehavethemunchies.tumblr.com/",
"updated": 1415895690,
"description": "<p>If any of you have any tasty recipes you wanna share just click submit~ If you are the owner of one of the images and wish to have it removed please message us and we will remove it quickly. Sorry for the inconvenience. </p>\n\n<p> If anything is tagged <strong>recipe</strong>, you can click through to the photos link for the recipe. If it is a flickr image, click through to the flickr image for a link directly to the recipe.\n<p><strong>Here are our most popular tags:</strong><p>\n\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/munchies\">Got the munchies?</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/Recipe\">Recipe</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/Pizza\">Pizza</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/Breakfast\">Breakfast</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/Lunch\">Lunch</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/Dessert\">Dessert</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/chocolate\">Chocolate</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/nutella\">Nutella</a>\n<p><a href=\"http://wehavethemunchies.tumblr.com/tagged/vegan\">Vegan</a>\n<p>\n<small>-4/13/09</small>",
"is_nsfw": false,
"ask": true,
"ask_page_title": "Ask me anything",
"ask_anon": false,
"submission_page_title": "Submit to your heart's content~",
"share_likes": false
}
}
}
Let's say I just want only map selected fields, like title and description from blog section. To do so, I've created java class which handles this request and creates Blog object, which has two fields, representing fields in JSON, which i want to map
import java.io.Serializable;
import org.json.JSONObject;
import com.google.gson.Gson;
public class HomeResponse implements Serializable{
public Blog blog;
public static HomeResponse fromJsonObject(JSONObject jsonObject){
Gson gson = new Gson();
return gson.fromJson(jsonObject.toString(), HomeResponse.class);
}
}
Object on which I want to map JSON:
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
public class Blog implements Serializable{
public String title;
public String description;
}
My question is: can I do this such way? Without creating all the other fields that are in JSON, and also ommitting "nodes" I don't need like meta etc.? Or I need to create Objects for all fields, which are in JSON i'm acquiring, even if I will not use need them in my code?
Yes That is perfectly okay to omit the properties you wish to ignore.
As a matter of facts, If you don't need a value, you shouldn't include a property for it, it will offer you better performance if anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With