Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON JObject.Parse modifies json string

Tags:

I have got incomming Json in format:

{
    "Type": "value",
    "Name": "MeteoStation",
    "UniqueAdress": "2C:3A:E8:0F:10:76",
    "valuesList": [{
        "Value": 23.00,
        "Unit": "C",
        "Type": "temperature",
        "SourceUniqAdress": "2C:3A:E8:0F:10:76",
        "TimeCaptured": "2018-03-26T09:36:13.200Z"
    }]
}

In my program, I want to create object IValuePacket that is one value in list of values.

JObject jobject = JObject.Parse(incomingJson);
var settings = new JsonSerializerSettings {
    NullValueHandling = NullValueHandling.Ignore,
    MissingMemberHandling = MissingMemberHandling.Ignore
};
var incommingMessage = JsonConvert.DeserializeObject<MessageEncapsulation>(incomingJson);
string Type = incommingMessage.Type;
string name = incommingMessage.Name;

if (string.IsNullOrWhiteSpace(name))
    name = "no name";

if (Type.ToLower().Equals("value")) {
    var values = JsonConvert.DeserializeObject<List<IValuePacket>>(jobject["valuesList"].ToString());
}

Everything works fine untill I recieved exact this json as mention above. JObject.Parse modifies value TimeCaptured and jobject looks like:

{
"Type": "value",
"Name": "Meteostation",
"UniqueAdress": "2C:3A:E8:0F:10:76",
"valuesList": [{
    "Value": 23.00,
    "Unit": "C",
    "Type": "temperature",
    "SourceUniqAdress": "2C:3A:E8:0F:10:76",
    "TimeCaptured": "2018-03-26T09:36:13.2Z"
}]}

Thats not so much difference, but DateTime.ParseExact(value, "yyyy-MM-ddThh:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture); cannot parse new value. Actually, I am sending 201 ms instead of 200 ms. It works, but I want to have good time for future reasons.

Is there any way how to avoid changing in Json during parsing?

like image 389
110mat110 Avatar asked Mar 26 '18 12:03

110mat110


People also ask

What does JObject parse do?

Jobject. Parse() method is an object class method and this method is used to parse the JSON string into the objects of C#. Based on the key value it parses the data of string and then it retrieves the data by using the key values.

How do I parse a string in JSON?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

What does JToken parse do?

JToken represents any possible token in a JSON file. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken. Parse() and get a result as long as the JSON is valid. JObject.

How does JSON parsing work?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.


1 Answers

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>>();
like image 177
Evk Avatar answered Sep 23 '22 12:09

Evk