JArray add values

0

How to add a set of JArray values to an existing json.

void adicionar(string name, string gamedir, string, lastVersionid, string javaArgs)
{
   /* adiciona ao json existente... */ 
}

Existing json code ...

{
  "profiles": {
    "nathan1302": {
      "name": "nathan1302",
      "lastVersionId": "1.8.8"
    },
    "Forge": {
      "name": "Forge",
      "lastVersionId": "1.7.10-Forge10.13.4.1566-1.7.10"
    },
    "OptiFine": {
      "name": "OptiFine",
      "lastVersionId": "1.8.8-OptiFine_HD_U_F5",
      "launcherVisibilityOnGameClose": "keep the launcher open"
    },
    "forge": {
      "name": "forge",
      "gameDir": "C:\Users\Nathan Ferreira\AppData\Roaming\.minecraft\222",
      "lastVersionId": "1.7.10-Forge10.13.4.1566-1.7.10",
      "javaArgs": "-Xmx5G -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -Xmn128M",
      "launcherVisibilityOnGameClose": "keep the launcher open"
    },
    "RFMinecraft": {
      "name": "RFMinecraft",
      "lastVersionId": "1.7.10-LiteLoader1.7.10-1.7.10-Forge10.13.4.1566-1.7.10",
      "javaArgs": "-Xmx3G",
      "useHopperCrashService": false,
      "launcherVisibilityOnGameClose": "keep the launcher open"
    } // ADICIONAR DEPOIS DAQUI!
  },
  "selectedProfile": "forge",
  "selectedUser": "fa944578e6f04db3b10ac85e81cf134d",
  "launcherVersion": {
    "name": "1.6.48",
    "format": 17
  }
}
    
asked by anonymous 10.12.2015 / 23:32

1 answer

1

If you have your JSON in a JObject variable (as you mentioned JArray , I'm assuming you're using the Newtonsoft.Json library, namespace Newtonsoft.Json.Linq), you can use code similar to below:

void adicionar(string name, string gamedir, string, lastVersionid, string javaArgs)
{
    JObject jsonExistente = getJsonExistente();
    JObject profiles = jsonExistente["profiles"] as JObject;
    JObject newProfile = new JObject();
    newProfile.Add("name", name);
    if (gamedir != null) newProfile.Add("gameDir", gamedir);
    if (lastVersionid != null) newProfile.Add("lastVersionId", lastVersionid);
    if (javaArgs != null) newProfile.Add("javaArgs", javaArgs);
    profiles.Add(name, newProfile);
    // salva o JSON existente se precisar
}
    
10.12.2015 / 23:44