Jarray add values

Asked

Viewed 75 times

0

How to add a set of values JArray in 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
  }
}
  • How are you loading existing JSON? And you mentioned that you have a "set of values JArray", what are these values?

  • So json is existing, this json is in case what would be in the file! I want q after Node RFMinecraft he inserts another Jproperty

  • Or whatever, the important thing is the original application of it recognize that it is a valid json!

1 answer

1


If you have your JSON in a variable JObject (as you mentioned JArray, I am assuming you are using the Newtonsoft.Json library, namespace Newtonsoft.Json.Linq), you can use a code similar to the one 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
}
  • Yes but as saved to a file?

  • File.WriteAllText(pathToFile, jsonExistente.ToString())

  • Thanks! It really worked!

Browser other questions tagged

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