0

I was programming my application when I wanted to make my program have the ability to change values in the JSON file using GSON. For example, if I had a JSON file with one of the keys "totalRaffles":"0" I would like to be able to change the integer, 0, to something else. In this case, this is my JSON file:

JSON File:

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

You would be given a shoe index (0, 1, or 2) to determine which Shoe's totalRaffles key you would want to change.

How would you do this?

2
  • Just out of curiosity why would you not change it before you convert this to JSON? Commented Jun 18, 2019 at 20:56
  • My application is one where the shoe raffles will continuously change perhaps every hour. When my application loads, it loads this file in, using the total raffles as one of the Shoes' properties. Therefore, I want this to change. Commented Jun 18, 2019 at 21:00

2 Answers 2

2

You need to parse it to an object, edit the field in the object and then serialize it again

If you need many changes you should do them all at once

When you parse the object it's better if you create a simple class to parse it

For the first one would some something like:

public class Shoes {Shoe [] shoes;}

for the Shoe:

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

To edit this go to the result of the deserialize that would be a Shoes object

Then find the one you need to edit

ShoesIntance.shoes[index].totalRaffles = 2;

Serialize it with gson and that should be it

Sign up to request clarification or add additional context in comments.

Comments

1

just use the addProperty(key, value) function, if the property key already exists it will override its value :)

So for example it would be:

 yourGsonArray.get(index).getAsJsonObject().addProperty("totalRaffles", 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.