java - Reading and appending to a json file -


i have json file follows:

{   "profiles": {     "example1": {       "name": "example1",       "lastversionid": "example1"     },     "example2": {       "name": "example2",       "lastversionid": "example2",       "playeruuid": "00000000000000000000000000000000"     },   },   "selectedprofile": "example",   "clienttoken": "000000000000000000000000000",   "authenticationdatabase": {     "000000000000000000000000000": {       "username": "example@gmail.com",       "accesstoken": "000000000000000000000000000",       "userid": "000000000000000000000000000",       "uuid": "000000000000000000000000000",       "displayname": "example"     }   } } 

i need append follows:

{   "profiles": {     "example1": {       "name": "example1",       "lastversionid": "example1"     },     "example2": {       "name": "example2",       "lastversionid": "example2",       "playeruuid": "00000000000000000000000000000000"     },     "example3": {       "name": "example3",       "lastversionid": "example3",     },   },   "selectedprofile": "example",   "clienttoken": "000000000000000000000000000",   "authenticationdatabase": {     "000000000000000000000000000": {       "username": "example@gmail.com",       "accesstoken": "000000000000000000000000000",       "userid": "000000000000000000000000000",       "uuid": "000000000000000000000000000",       "displayname": "example"     }   } } 

so basically, need add profile file.

here current unsuccessful code format not write correctly file.

package minecraftmpc;  import java.io.filenotfoundexception; import java.io.filereader; import java.io.filewriter; import java.io.ioexception;  import org.json.simple.jsonobject; import org.json.simple.parser.jsonparser; import org.json.simple.parser.parseexception;  public class profile {      public static void main(string[] args) throws filenotfoundexception, ioexception, parseexception {         profileappend("test", "examplename");     }      @suppresswarnings("unchecked")     public static void profileappend(string profile, string version)             throws filenotfoundexception, ioexception, parseexception {          jsonparser parser = new jsonparser();         object obj = parser.parse(new filereader(getproperties.mcdir()                 + "/launcher_profiles.json"));         jsonobject jsonobject = (jsonobject) obj;         jsonobject profiles = (jsonobject) jsonobject.get("profiles");         string selectedprofile = profile;         string clienttoken = (string) jsonobject.get("clienttoken");         jsonobject authenticationdatabase = (jsonobject) jsonobject                 .get("authenticationdatabase");          jsonobject params = new jsonobject();          params.put("lastversionid", version);         params.put("name", profile);         profiles.put(profile, params);          jsonobject test = new jsonobject();         test.put("profiles", profiles);         test.put("selectedprofile", selectedprofile);         test.put("clienttoken", clienttoken);         test.put("authenticationdatabase", authenticationdatabase);          filewriter file = new filewriter(getproperties.mcdir()                 + "/launcher_profiles-test.json");         file.write(test.tojsonstring());         file.flush();         file.close();          system.out.println(test);     } } 

output:

{"selectedprofile": "example", "profiles": {"example1": { "name": "example1","lastversionid": "example1"}, "example2": {"name": "example2", "lastversionid": "example2", "playeruuid": "00000000000000000000000000000000" }, "example3": {"name": "example3", "lastversionid": "example3",}, },"clienttoken": "000000000000000000000000000", "authenticationdatabase": { "000000000000000000000000000": { "username": "example@gmail.com", "accesstoken": "000000000000000000000000000", "userid": "000000000000000000000000000", "uuid": "000000000000000000000000000", "displayname": "example" } } } 

your output expected, formatted 1 line instead of multiple ones.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -