英文:
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();
}
第二个文件如下所示:
文件 ----> 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-----> ProductController.Java 
@GetMapping("/products_from_db")
        public  List<Product> getProducts_from_db() { 
            return productservice.findAll() ;
        } 
Second File is as Follows.
File ----> 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 +
                '}';
    }
output:
[{
	"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 display only name and price in back end code only.
答案1
得分: 0
很遗憾,Gson在某些方面并不像Jackson那样直接 - 在Jackson中,你可以简单地使用注释 @JsonIgnore。
另一方面,Gson似乎更具灵活性(尽管有时会过于技术性)。
对于Gson,你有两个主要选项来实现你想要的效果:
- 
将要忽略的字段标记为
transient:private transient String id(在这种情况下,你必须确保这不会在你的应用程序中引起任何副作用。) - 
你可以充分利用Gson的灵活性,特别是注释
@Expose。例如:@Expose(Serialize = false)
private String id; 
你可以在这里找到更多示例和详细信息 here 和 here。
如果有帮助,请告诉我们。
英文:
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:
- 
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.) - 
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论