Reputation: 2862
I have this json
object tmp = new
{
name = Name,
type = Type,
parentId = ParentId,
Location= string.Format("[\"{0}\"]" ,Location1)
};
string json = JsonConvert.SerializeObject(tmp);
This string is casuing the problem
string Location = string.Format("[\"{0}\"]" ,Location1)
The result is
{...,"Location":"[\"Location5201\"]"}
If i get rid of the \
Then the output is
{...,"Location":"[Location5201]"}
My desired output should be
{...,"Location":["Location5201"]}
How can i put ""
into the string above?
Upvotes: 0
Views: 84
Reputation: 174660
["something"]
is JSON for "an array of one string":
Location = new []{ Location1 }
If Location1
is not already of type string
:
Location = new []{ Location1.ToString() }
or
Location = new []{ string.Format("{0}", Location1) }
Upvotes: 3
Reputation: 34224
The thing is that Location
property is actually an array of strings, but not a string.
You don't need to form it with string yourself. Instead, you need to declare it as an array:
Location = new[] { Location1 }
You might want to spend more time on reading about JSON format and its serialization in C# before trying to achieve something with code in an initially incorrect way.
Upvotes: 5