#JSON write/read

1 messages · Page 1 of 1 (latest)

quiet sky
#
public class Main {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        JSONObject object = new JSONObject();
        object.put("name", "Walter");
        object.put("age", 20);
        object.put("job", "gamer");
        try{
            FileWriter file = new FileWriter("mydata.json");
            file.write(object.toJSONString());
            file.flush();
            file.close();
        }catch (Exception e){
            e.getStackTrace();
        }
    }
}

mydata.json is in the same folder as Main class. Nothing happens.
If this library isn't good, recommend another one please.

verbal wraithBOT
#

<@&987246399047479336> please have a look, thanks.

steel topaz
steel topaz
gloomy rampart
gloomy rampart
empty anchor
#

it is still not correct to read and write a file in resources

gloomy rampart
quiet sky
steel topaz
quiet sky
steel topaz
steel topaz
#

why would it create a file in the source code of your project ?

#

it doesn't make sense

quiet sky
quiet sky
steel topaz
#

Let's say you are making an app

#

so it ends up into an exe

#

you are basically saying that you want to add a file into the exe at runtime

quiet sky
quiet sky
#

But i totally get your point.

quiet sky
# steel topaz yes

Ok, i got everything you recommended. Time to try it out. Thank you for help and explanation!

steel topaz
#

you need to create a class which represents your json

warm kite
#

you can use readTree

#

which will return JsonNode

steel topaz
warm kite
#

well im not saying they should use that

#

just that it exists

#

imagine you have some giant json youre fetching, but need only 1 field one time, do you deserialize the whole thing into pojo?