I have json example "TestConfigIdMapping.json":
{
"schema": "None",
"TestMapping": [
{
"testId": "test_1",
"testConfigs": [
"platform1",
"platform2",
"platform3",
]
},
{
"testId": "test_2",
"testConfigs": [
"platform2"
]
}
]
}
Using Newtonsoft.Json c# library, how would I add json object element:
{
"testId": "test_3",
"testConfigs": [
"platform1",
"platform3"
]
}
to the above already loaded json file?
My code using form1 and display json in form2:
class JsonTestMapping
{
[JsonProperty("schema")]
string Schema = "None";
[JsonProperty("TestConfigMapping")]
public JsonObjects[] testConfigMapping { get; set; }
}
class JsonObjects
{
[JsonProperty("testId")]
public string TestId { get; set; }
[JsonProperty("testConfigs")]
public string[] TestConfigs { get; set; }
}
class LoadJson
{
public string jsonValues = File.ReadAllText(@"C:\GITTest\TestIdConfigMapping.json");
}
JsonObjects jsonObjects = new JsonObjects()
{
TestId = f1.Form1textbox.Text,
TestConfigs = new string[]
{
// set FormTextbox2 modifier = public
f1.FormTextbox2.Text
}
};
The following is Form2 popup and displays json:
private void Form2_Load(object sender, EventArgs e)
{
Form2TextBox.ReadOnly = true;
Form2TextBox.Multiline = true;
Form2TextBox.ScrollBars = ScrollBars.Vertical;
Form2TextBox.WordWrap = true;
//string convertJsonFileSerialized = JsonConvert.SerializeObject(readJsonFile.jsonText);
//List<ReadJsonFile> convertJsonFileDeserialized = JsonConvert.DeserializeObject<List<ReadJsonFile>>(readJsonFile.jsonText);
//string finalJson = JsonConvert.SerializeObject(convertJsonFileDeserialized);
LoadJson loadJson = new LoadJson();
string myJsonValues = loadJson.jsonValues;
JObject rss = JObject.Parse(myJsonValues);
JArray TestConfigMapping = (JArray)rss["TestConfigMapping"];
//List<JsonObjects> myList = JsonConvert.DeserializeObject<List<JsonObjects>>(myjsonRead);
JsonObjects jsonObjects = new JsonObjects()
{
TestId = f1.Form1textbox.Text,
TestConfigs = new string[]
{
// set FormTextbox2 modifier = public
f1.FormTextbox2.Text
}
};
List<JsonObjects> jsonList = new List<JsonObjects>();
jsonList.Add(jsonObjects);
string jsonResult1 = JsonConvert.SerializeObject(jsonObjects, Formatting.Indented);
JsonTestMapping jsonTestConfigMapping = new JsonTestMapping()
{
testConfigMapping = new JsonObjects[]
{
jsonObjects
}
};
string jsonResult = JsonConvert.SerializeObject(jsonTestConfigMapping, Formatting.Indented);
//ReadJsonFile readJsonFile = new ReadJsonFile();
//string jsonResult2 = readJsonFile.jsonText;
Form2TextBox.Text = rss.ToString();
}
The goal is to have Form 1, with 2 text box inputs that insert the "TestID" & "TestConfigs". Upon clicking button1, convert the 2 input texts into Json format and upon clicking "append" button, asserts the formatted Json object element into the already existing json file configuration.