gpt4 book ai didi

java - 如何使用 GSON 编辑/更改 JSON 文件中的值?

转载 作者:行者123 更新时间:2023-12-01 19:38:04 24 4
gpt4 key购买 nike

当我想让我的程序能够使用 GSON 更改 JSON 文件中的值时,我正在编写应用程序。例如,如果我有一个带有键 "totalRaffles":"0" 之一的 JSON 文件,我希望能够将整数 0 更改为其他值。在本例中,这是我的 JSON 文件:

JSON 文件:

{
"shoes": [
{
"shoeName": "Nike React Presto",
"shoePrice": "120",
"brand": "Nike",
"typeOfShoes": "Running",
"style": "Men's Shoe",
"colors": [
"Blue",
"Green",
"Pink",
"Aqua",
"Yellow"
],
"sizes": [
"4",
"4.5",
"5"
],
"description": "Inspired by the early 2000s original, the Nike Presto React puts an exaggerated spin on an unconventional icon. Nike React foam delivers an expressive look with a lightweight, bouncy feel and a whole lot of personality.",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "5",
"totalRaffles": "0",
"imageURLs": [
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/zl49g5yxcibxdrsigngb/react-presto-mens-shoe-c4Rbf6.jpg",
"https://c.static-nike.com/a/images/t_prod_ss/w_960,c_limit,f_auto/jablbmfxdynwjvbgxzh1/nike-react-presto-brutal-honey-release-date.jpg",
"https://c.static-nike.com/a/images/t_prod_ss/w_960,c_limit,f_auto/jqzhsqisjcmbhozrwefr/nike-react-presto-brutal-honey-release-date.jpg",
"https://c.static-nike.com/a/images/t_prod_ss/w_960,c_limit,f_auto/tq0vsbg8yaa3ojia8fkx/nike-react-presto-brutal-honey-release-date.jpg"
],
"isSold": "false"
},
{
"shoeName": "Nike Air Zoom Pegasus 36 Trail",
"shoePrice": "130",
"brand": "Nike",
"typeOfShoes": "Running",
"style": "Women's Shoe",
"colors": [
"Yellow",
"Green",
"Aqua",
"Blue",
"Pink"
],
"sizes": [
"5",
"5.5",
"6"
],
"description": "An icon hits the paths less traveled in the Nike Air Zoom Pegasus 36 Trail. Perforated mesh upper offers breathable comfort, and double Zoom Air units cushion your stride. Outsole lugs optimize traction when running uphill.",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "0",
"totalRaffles": "0",
"imageURLs": [
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/d6yfuqvqp7l6os3oswll/air-zoom-pegasus-36-trail-womens-running-shoe-vF7D4W.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/bnboer2ecdimuy693qw1/air-zoom-pegasus-36-trail-womens-running-shoe-vF7D4W.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/znolfda4rdgvxcaz1dpl/air-zoom-pegasus-36-trail-womens-running-shoe-vF7D4W.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/qutm7re9hzxoua97vund/air-zoom-pegasus-36-trail-womens-running-shoe-vF7D4W.jpg"
],
"isSold": "false"
},
{
"shoeName": "Nike Air Zoom Wildhorse 5",
"shoePrice": "110",
"brand": "Nike",
"typeOfShoes": "Running",
"style": "Men's Shoe",
"colors": [
"Pink",
"Aqua",
"Red",
"Black",
"Gray"
],
"sizes": [
"6",
"6.5",
"7",
"7.5",
"8"
],
"description": "Built specifically for trails, the Nike Air Zoom Wildhorse 5 keeps you galloping over rough terrain in breathable, multi-layer fabric. A rock plate helps shield your foot, while a Zoom Air heel unit cushions your stride on and off the path.",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "5",
"totalRaffles": "0",
"imageURLs": [
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/podtc6tfkrrrdtm9br5k/air-zoom-wildhorse-5-mens-running-shoe-lMslPS.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/byjlvjfbmcvzq7vcieze/air-zoom-wildhorse-5-mens-running-shoe-lMslPS.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/r9jwzrbf0uxuspoorq6f/air-zoom-wildhorse-5-mens-running-shoe-lMslPS.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/cmbqldjtyaocthwmk8xl/air-zoom-wildhorse-5-mens-running-shoe-lMslPS.jpg"
],
"isSold": "false"
}
]
}

您将获得一个鞋子索引(0、1 或 2)来确定您想要更改哪只鞋子的 TotalRaffles 键。

你会怎么做?

最佳答案

需要将其解析为对象,编辑对象中的字段,然后再次序列化

如果您需要进行多项更改,则应该一次完成所有更改

当您解析对象时,最好创建一个简单的类来解析它

对于第一个,会是这样的:

public class Shoes {Shoe [] shoes;}

对于鞋子:

public class Shoe {
String shoeName;
String shoeSize;
int totalRaffles;
...
}

要编辑此内容,请转到反序列化的结果,该结果将是 Shoes 对象

然后找到您需要编辑的内容

ShoesIntance.shoes[index].totalRaffles = 2;

用gson序列化一下就可以了

关于java - 如何使用 GSON 编辑/更改 JSON 文件中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com