Use the JObject class in Newtonsoft.Json.Linq to do this without knowing the JSON structure beforehand:
using Newtonsoft.Json.Linq; string jsonString = File.ReadAllText("myfile.json"); JObject jObject = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString) as JObject;
Example:
string jsonString = "{\"Admins\":[\"234567\"],\"ApiKey\":\"Text\",\"mainLog\":\"syslog.log\",\"UseSeparateProcesses\":\"false\",\"AutoStartAllBots\":\"true\",\"Bots\":[{\"Username\":\"BOT USERNAME\",\"Password\":\"BOT PASSWORD\",\"DisplayName\":\"TestBot\",\"Backpack\":\"\",\"ChatResponse\":\"Hi there bro\",\"logFile\":\"TestBot.log\",\"BotControlClass\":\"Text\",\"MaximumTradeTime\":180,\"MaximumActionGap\":30,\"DisplayNamePrefix\":\"[AutomatedBot] \",\"TradePollingInterval\":800,\"LogLevel\":\"Success\",\"AutoStart\":\"true\"}]}"; JObject jObject = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString) as JObject; // Update a string value; JToken jToken = jObject.SelectToken("Bots[0].Password"); jToken.Replace("password"); // Update an integer value: JToken jToken2 = jObject.SelectToken("Bots[0].TradePollingInterval"); jToken2.Replace(555); // Update a boolean value: JToken jToken3 = jObject.SelectToken("Bots[0].AutoStart"); jToken3.Replace(false); // Get an indented/formatted string: string updatedJsonString = jObject.ToString(); //Output: //{ // "Admins": [ // "234567" // ], // "ApiKey": "Text", // "mainLog": "syslog.log", // "UseSeparateProcesses": "false", // "AutoStartAllBots": "true", // "Bots": [ // { // "Username": "BOT USERNAME", // "Password": "password", // "DisplayName": "TestBot", // "Backpack": "", // "ChatResponse": "Hi there bro", // "logFile": "TestBot.log", // "BotControlClass": "Text", // "MaximumTradeTime": 180, // "MaximumActionGap": 30, // "DisplayNamePrefix": "[AutomatedBot] ", // "TradePollingInterval": 555, // "LogLevel": "Success", // "AutoStart": false // } // ] //}
source share