Reputation: 798
I am trying to create JsonObject with the below-mentioned structure.
{
"id": "1",
"name": "XXX",
"age": "30"
}
Using the code,
dynamic sampleJson = new JObject();
sampleJson.Add("id", "1");
sampleJson.Add("name", "XXX");
sampleJson.Add("age", "30");
But the problem is that extra curly braces is appearing at the begining and end of the json structure, as shown below.
{{
"id": "1",
"name": "XXX",
"age": "30"
}}
I use the required JSON structure as the post body of an API and it should be in the JSON format(So cant use JSON string structure using ToString() method).How can I remove the extra braces and achieve my requirement ???
Upvotes: 7
Views: 16095
Reputation: 1039
ToString() didn't work for me. The following did:
JsonConvert.DeserializeObject(JSONStringHere, typeof(ExpandoObject));
Upvotes: 7
Reputation: 118947
Since you are using a JObject
, you can simply call the ToString()
override to create your JSON. For example:
JObject sampleJson = new JObject();
sampleJson.Add("id", "1");
sampleJson.Add("name", "XXX");
sampleJson.Add("age", "30");
var json = sampleJson.ToString();
Now your json
variable will contain:
{
"id": "1",
"name": "XXX",
"age": "30"
}
Upvotes: 4