persistent quotes

Oct 14, 2010 at 7:45 PM
Edited Oct 18, 2010 at 2:29 PM

I have the following json string:

 

{"a":"apple","b":"banana","c":"cherry"}

 

which I'm looping through with the following code:

 

JsonConverter[] converters = new JsonConverter[1];
converters[0] = new KeyValuePairConverter();

JObject json = JObject.Parse(message);

foreach (KeyValuePair<string, JToken> pair in json)
{
	Trace.WriteLine(pair.Key + " " + pair.Value);
	Trace.WriteLine(pair.Key + " " + pair.Value.ToString());
	Trace.WriteLine(pair.Key + " " + pair.Value.ToString(Newtonsoft.Json.Formatting.None, converters));
}

 

and I'm seeing this output:

a "apple"
a "apple"
a "apple"
b "banana"
... 

Why is "apple" surrounded by quotes?

Coordinator
Oct 14, 2010 at 9:40 PM

ToString on a JToken encodes it as it would be displayed as JSON, reguardless of whether it is an JSON object or a JSON string.

To get the underlying value either cast the JToken to a JValue and access the JValue.Value property or cast the JToken directly to a string.