#Edit a json Object
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
what have you got so far?:
have you loaded the json file?
ok and now what do you want to do with that HashMap?

why do you even have a HashMap then?
why are you putting the content of the Json into a HashMap and not stick to the returned Json from Json.read?
?
cant you tell me the reason for the HashMap?
ok and you updating the values and now want to writ ethe content of the HashMap back to the file?
in the json or in the hashmap?
then you need to write it to the file back
couldnt find a way to just edit one line
in his tutorial
but just wait for him ig
you already pinged him
and I think its night time for him
I think thats because if writing in a file to java you would always need to write the whole file and cant edit just a single line without writing the old content as well
it was smth like that
yeah, opening for writing takes the longest time anyways
So writing one line or ten shouldn’t really matter at this scale
Format the code next time
it actually might not, I am not really into this topic but I think internally the buffer pointer gets set to the end
but not sure 🤷♂️
yes
write the whole hash map back out
yep
edit the hashmap
okay so you have your hashmap<String, String[]>
you need to get it to a Json object again
HashMap<String, String[]> m = ...;
var array = m.get("example");
if (array != null) {
array[3] = "2";
}
so that will edit the array in your hashmap
(the way you are describing it)
then just write it back out
override your json with the HashMap
Hm?
the write methods accepts an Json object
so how to go from HashMap<String, String[]> -> Json?
^
(have to acknowledge that HashMap<String, String[]> is a bit cookoo as an application model)
btw cant you edit the content in the Json object directly?
so there is no need for a HashMap?
Json model is immutable. You can clone it into a builder and edit the builder, but "Json -> X, edit X, X -> Json" is what I was thinking of
well - do you have a write method already?
can you show it
I said write
not read
okay
so use Json.objectBuilder() and Json.arrayBuilder()
loop over your hashmap, and for each entry add to the builders
dont you have a of method for map?
yep
if they have a method to at least encode a String[] that will work
but doing the normal loop I think is what they should do
just to talk about a small thing - json, as a data format, is especially poorly suited to "update just one part without writing the whole thing out again"
i'm sure its doable in some way
but in general your mode of interaction is going to be "load thing into memory", followed by "edit it", then "write out again"
check the github/javadocs ig
a JsonEncodable is an interface which contains a toJson method
Use Json.arrayBuilder() and a for loop
if you can write a method that just does String[] -> Json we can make use of that to do the whole hashMap
okay
focus on this
static Json stringArrayToJson(String[] strings) {
}
Use Json.arrayBuilder() and a for loop