How to write to an .json file

0

I have some files .json in assets .

I'mcurrentlyjustreadingthesefiles.

Reading

publicStringloadJSONFromAsset(StringAjson){Stringjson=null;try{InputStreamis=context.getAssets().open(Ajson);intsize=is.available();byte[]buffer=newbyte[size];is.read(buffer);is.close();json=newString(buffer,"UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }

    return json;
}

How can I write to them? That is, replace all of your content with another of my choice.

    
asked by anonymous 19.02.2017 / 06:26

1 answer

1

Cristian,

Ideally, you would use a JSON parser that will make it easier to manipulate this modified object than simply edit the text of the file.

On the official website you can find several packages link

Download a package, for example: link

Import to your project and follow the usage example:

import org.json.JSONObject;

//instancia um novo JSONObject
JSONObject my_obj = new JSONObject();

//preenche o objeto com os campos: titulo, ano e genero
my_obj.put("titulo", "JSON Parser Exemplo");
my_obj.put("ano", 2017);
my_obj.put("genero", "Programação");

This will make it easier for you to edit your json. More information:

link

I hope this helps ...

Good Luck!

    
19.02.2017 / 07:09