Gson在Java中的JSON解析

huangapple go评论75阅读模式
英文:

Gson Json Parsing in Java

问题

以下是翻译后的内容:

我想解析 JSON 列表。我已经使用了 Spring Boot 项目,数据库是 H2。数据库中包含了一些数据。

我已经成功地通过 @GetMapping 函数显示了 JSON 数据。
代码如下:

文件-----> ProductController.java 
@GetMapping("/products_from_db")
public List<Product> getProducts_from_db() { 
    return productservice.findAll();
}

第二个文件如下所示:

文件 ----&gt; Product.java

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
//@ToString
@Entity
public class Product {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private int price;

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

输出结果:

[{
    "id": 1,
    "name": "Mango",
    "price": 98
}, {
    "id": 2,
    "name": "Strawberry",
    "price": 92
}, {
    "id": 3,
    "name": "Cadbury",
    "price": 85
}, {
    "id": 4,
    "name": "yash",
    "price": 100
}]

我想在后端代码中只显示名称和价格。

英文:

I want to parse json list. I have used springboot project, database is H2. Database contains few data.

I have successfully displayed json data via @getmapping function.
Code is as below,

File-----&gt; ProductController.Java 
@GetMapping(&quot;/products_from_db&quot;)
        public  List&lt;Product&gt; getProducts_from_db() { 
            return productservice.findAll() ;
        } 

Second File is as Follows.

File ----&gt; Product.java

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
//@ToString
@Entity
public class Product {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private int price;

    @Override
    public String toString() {
        return &quot;Product{&quot; +
                &quot;id=&quot; + id +
                &quot;, name=&#39;&quot; + name + &#39;\&#39;&#39; +
                &quot;, price=&quot; + price +
                &#39;}&#39;;
    }

output:

[{
	&quot;id&quot;: 1,
	&quot;name&quot;: &quot;Mango&quot;,
	&quot;price&quot;: 98
}, {
	&quot;id&quot;: 2,
	&quot;name&quot;: &quot;Strawberry&quot;,
	&quot;price&quot;: 92
}, {
	&quot;id&quot;: 3,
	&quot;name&quot;: &quot;Cadbury&quot;,
	&quot;price&quot;: 85
}, {
	&quot;id&quot;: 4,
	&quot;name&quot;: &quot;yash&quot;,
	&quot;price&quot;: 100
}]

I want to display only name and price in back end code only.

答案1

得分: 0

很遗憾,Gson在某些方面并不像Jackson那样直接 - 在Jackson中,你可以简单地使用注释 @JsonIgnore

另一方面,Gson似乎更具灵活性(尽管有时会过于技术性)。

对于Gson,你有两个主要选项来实现你想要的效果:

  1. 将要忽略的字段标记为 transientprivate transient String id(在这种情况下,你必须确保这不会在你的应用程序中引起任何副作用。)

  2. 你可以充分利用Gson的灵活性,特别是注释 @Expose。例如:

    @Expose(Serialize = false)
    private String id;

你可以在这里找到更多示例和详细信息 herehere

如果有帮助,请告诉我们。

英文:

Unfortunately Gson isn't as straight forward in certain aspects as, for example, Jackson - where you can simply use the annotation @JsonIgnore.

On the other hand, Gson seems to have more flexibility (although a little too technical sometimes.)

In the case of Gson you have two main options to achieve what you want:

  1. Mark the field you want to ignore as transient: private transient String id (in this case you have to make sure that this won't cause any side effects in your application.)

  2. You can use the very flexible capabilities of Gson, especially the annotation @Expose. for example:

    @Expose(Serialize = false)
    private String id;

You can find more examples and details here and here.

Please, let us know if it helps.

huangapple
  • 本文由 发表于 2020年5月30日 01:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/62091297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定