JSON JObject.Parse modifies json string
It does not really modify your string, it just parses your date string into DateTime
object when you call JObject.Parse
. If you do this:
var obj = JObject.Parse(json);var values = (JArray) obj["valuesList"];var time = (JValue) values[0]["TimeCaptured"];Console.WriteLine(time.Value.GetType());
You notice that time.Value
is of type DateTime
. Then you do this:
JsonConvert.DeserializeObject<List<IValuePacket>>(jobject["valuesList"].ToString());
By doing that you convert valueList
back to json, but now TimeCaptured
is DateTime
and not a string, so that DateTime
object is converted to json string using whatever date time format is used by JSON.NET by default.
You can avoid parsing strings that look like dates to .NET DateTime
objects by parsing json to JObject
like this:
JObject obj;using (var reader = new JsonTextReader(new StringReader(json))) { // DateParseHandling.None is what you need reader.DateParseHandling = DateParseHandling.None; obj = JObject.Load(reader);}
Then type of TimeCaptured
will be string, as you expect.
Side note: there is no need to convert JToken
back to string and then call JsonConvert.Deserialize
on that string. Instead, do this:
var values = obj["valuesList"].ToObject<List<IValuePacket>>();