persistent quotes

Oct 14, 2010 at 6:45 PM
Edited Oct 18, 2010 at 1:29 PM

I have the following json string:




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?

Oct 14, 2010 at 8: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.