当前位置: 首页 > 知识库问答 >
问题:

如何使用gson将选定字段从JSON映射到Java对象

汪典
2023-03-14

我使用gson在Java对象上映射JSON。我有一个类似于下面示例的JSON

{
    "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
        }
    }
}

假设我只想映射选定的字段,比如博客部分的标题和描述。为此,我创建了java类来处理这个请求,并创建了Blog对象,它有两个字段,用JSON表示字段,我想映射它们

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);
    }

}

要映射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中的所有字段创建对象,即使我不会在代码中使用它们?

共有1个答案

伊裕
2023-03-14

是的,省略您希望忽略的属性是完全可以的。

事实上,如果你不需要一个值,你不应该为它包含一个属性,它会给你提供更好的性能。

 类似资料:
  • 问题内容: 我们有一张有很多列的大桌子。移至MySQL Cluster后,由于以下原因无法创建表: 错误1118(42000):行大小太大。不包括BLOB在内的已使用表类型的最大行大小为14000。这包括存储开销,请查阅手册。您必须将某些列更改为TEXT或BLOB 举个例子: 这是用于存储配置参数的表。我在想,我们可以将一些列合并为一个列,并将其存储为JSON对象,然后将其转换为Java对象。 例

  • 问题内容: 我有以下JSON表示盐请求的服务器响应: 我尝试使用以下POJO映射它: 现在每次我这样做: 该为空。如何使用Gson将JSON映射到POJO?我的变量顺序对Gson映射重要吗? 问题答案: 我的变量顺序对Gson映射重要吗? 不,不是这样。 如何使用Gson将JSON映射到POJO? 它是 区分大小写 和JSON字符串键应该是相同的POJO类使用的变量名。 您可以使用@Seriali

  • 我有以下JSON来表示salt请求的服务器响应: 我尝试用以下POJO映射它: 每次我这样做: 为空。如何使用GSON将JSON映射到POJO?变量的顺序对Gson映射重要吗?

  • 本文向大家介绍apache-spark 使用Gson将JSON映射到自定义类,包括了apache-spark 使用Gson将JSON映射到自定义类的使用技巧和注意事项,需要的朋友参考一下 示例 使用Gson,您可以读取JSON数据集并将其映射到自定义类MyClass。 由于Gson不可序列化,因此每个执行程序都需要自己的Gson对象。而且,MyClass必须可序列化才能在执行程序之间传递。 请注意

  • 我需要将多个dto映射到实体。几乎所有的dto都有一个指向另一个dto的引用,我不知道在映射dto时如何传递引用。 以下是该地址的一个业务实现: 这是地图绘制者 所有实现者都从同一个dto读取,然后将它们映射到dto和entites。 以下是针对客户实施的建议: 以及我希望引用映射地址实体的映射(我的客户实体有一个地址类型字段)。 非常感谢你。

  • 我们有一张有很多柱子的大桌子。在我们移动到MySQL集群后,表无法创建,因为: 错误1118 (42000):行大小过大。使用的表类型(不包括BLOB)的最大行大小为14000。这包括存储开销,检查手册。您必须将一些列更改为TEXT或BLOB 例如: 它是一个用于存储配置参数的表。我在想,我们可以将一些列组合成一个列,并将其存储为JSON对象,并将其转换为Java对象。 例如: 我们定义了: 通过