Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.NET JsonConvert.DeserializeObject() return null value

i tried to Deserialize this string :

string _jsonObject = {\"Ad\":{\"Type\":\"Request"\,
         \"IdAd\":\"[email protected]\",
         \"Category\":\"cat\",
         \"SubCategory\":\"subcat\"},
\"Position\":{\"Latitude\":\"38.255\",
              \"Longitude\":\"1.2\",
              \"Imei\":\"0123456789\"};
}";

Message _message = JsonConvert.DeserializeObject<Message>(_jsonObject);

Works pretty for "Ad" but not instanciate "Position". Any idea ?

like image 229
JossVAMOS Avatar asked Jun 17 '14 14:06

JossVAMOS


1 Answers

I've never had any issues using Newtonsoft.Json, but decided to go with built in json libraries in latest project. Ended up with null result. Turns out the following will fail:

JSON:

{
   "myProperty": "abc"
}

CLASS:

public void MyClass
{
   public string MyProperty { get; set; }
}

Why does it fail? "myProperty" in json is camel case (starts with lower case letter), while MyProperty in MyClass starts with upper case letter. If you make both cases the same it works. I tried figuring out how to configure case insensitivity for the entire app, but apparently that's not possible to do, so I went back to Newtonsoft.JSON and the problem went away.

like image 99
Eternal21 Avatar answered Sep 27 '22 23:09

Eternal21