Zakaria
Zakaria

Reputation: 15070

Deserialize a Json string (in C#) without Json.Net

I use the following lines to deserialize simple Json strings like this:

string jSon = "{\"FirstName\":\"Foo\", \"LastName\":\"Bar\"}";
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Person personDeserialized = oSerializer.Deserialize<Person>(jSon);

Then, I add the "personDeserialized" object to the database with Entity Framework.

The problem is that this method doesn't work if I have the following data :

string jSon = "{
   \"FirstName\":\"Foo\", 
   \"LastName\":\"Bar\",
   \"Hobbies\":
   [
      {\"Sport\":\"FootBall\"},
      {\"Music\":\"Rock\"},
   ]
}";

Of course, the Person class contains references to the Hobbie's one.

So, is there a way, and without the jSon.NET library, to add the "Hobbies" object automatically to the personDeserialized object?

Thanks,

Regards.

Upvotes: 3

Views: 436

Answers (1)

Guilherme Duarte
Guilherme Duarte

Reputation: 3441

Have you tried using the new .NET DataContractJsonSerializer? I believe I works much better than the JavaScriptSerializer that you're using.

Upvotes: 2

Related Questions