Include and remove elements from a Json with lib Gson (Java)

Asked

Viewed 264 times

2

I am using the Gson library in Java, I have the following structure in a json file:

{"idAluno":4, "nomeAluno":"Teste da Silva", "listaDeTurmas":[1,2,4]}

Is it possible, with this library, to change the entry "listDeTurmas" to include/remove values from it?? For example, add the value 3 or remove the value 4. Or, there is some other Json manipulation library that provides me with a function for this?

  • Remember that the idea is not to deserialize the record, change it and then insert it in the json file back. Because the file has several records like this, and it would be necessary to rewrite it whole by putting the record in modified question, since it is not possible to change only one line of the file (or is it?). It seems to me that the effort for a relatively simple operation will be carried out several times.

  • You can convert json to a java class with gson and manipulate it normally and, if necessary, turn it into json again with gson

  • @Guilhermecostamilam Yes, but after transforming the changed record into json again, to put it back in the file I will have q rewrite the entire file (all other records). It would be similar to a text file deletion operation. You can’t edit just one line. Or I’m wrong?

  • Basically you read a file . json changes an array within it and returns the new file . json, that?

  • Yes. Each line in the file has a record like the example post. Within a specific line, I want to change only the values of the listed id.

  • 1

    Here has a question on guj that can help you (no gson)

  • Take a look at the json.org library, it should suit you.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.