views:

62

answers:

1

Hi everybody,

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.

+2  A: 

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

gjsduarte