0

So I have been working on JSON files over the past few days, just to understand how C# can manipulate JSON.

I need some help with adding a property to a JSON file. I have figured out how to edit values through trial and error, however, I had assumed that it would create the path where I access the value. Turns out this isn't the case.

This is what I currently have done with regards to access a values Height and Width, however, there is no "externalSite""Weblogin""window" path within the JSON

string widthBox = Width.Text.ToString();
string heightBox = Height.Text.ToString();

string CustomSizejson = File.ReadAllText(DownloadConfigFilelocation);
JObject CustomSizeobj = JObject.Parse(CustomSizejson);
CustomSizeobj["externalSite"]["webLogin"]["window"] = "height=" + heightBox + ",width=" + widthBox + ",resizable,scrollbars";
string CustomSizenewJson = CustomSizeobj.ToString();
File.WriteAllText(DownloadConfigFilelocation, CustomSizenewJson);

This is pretty much what I want to accomplish and append it to the JSON file

EXPECTED RESULT

Can someone help me figure this out? Thanks

2
  • You need to create the objects in the JSON tree one by one. They’re not created automatically when you try to access them Commented Jul 23, 2018 at 7:21
  • Yeah I realized this - Trying to figure out the way to implement it Commented Jul 23, 2018 at 7:24

2 Answers 2

1

You can add property using JObject.Add function. Example:

JObject json = new JObject();
json.Add("property_name", "property_value");

If you want to add something not exactly object but inside some property of it. You first need to find property itself and use JObject.Add function. Example:

JObject inner_json = (JObject) json["property_name"];
inner_json.Add("inner_property_name","value");
Sign up to request clarification or add additional context in comments.

Comments

0

You can try something like:

var input = new JObject();
input.Add("window", "height=300,width=410,resizable,scrollbars");

var obj = new JObject();
obj.Add("webLogin", input);

var obj1 = new JObject();
obj1.Add("externalSite", obj);

For more information: JObject nested property

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.