如何使用gson将选定字段从JSON映射到Java对象
我使用gson在Java对象上映射一个JSON。我有一个类似于下面示例的JSON如何使用gson将选定字段从JSON映射到Java对象,java,json,gson,Java,Json,Gson,我使用gson在Java对象上映射一个JSON。我有一个类似于下面示例的JSON { "meta": { "status": 200, "msg": "OK" }, "response": { "blog": { "title": "We have the Munchies.", "name": "wehavethemunchies", "posts":
{
"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
}
}
}
要在其上映射JSON的对象:
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
public class Blog implements Serializable{
public String title;
public String description;
}
我的问题是:我可以这样做吗?如果不创建JSON中的所有其他字段,也不匹配“节点”,我就不需要像meta这样的字段了。?或者我需要为我正在获取的JSON中的所有字段创建对象,即使我不会在代码中使用它们?是的,省略您希望忽略的属性完全可以 事实上,如果你不需要一个值,你不应该为它包含一个属性,它会为你提供更好的性能
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
public class Blog implements Serializable{
public String title;
public String description;
}