Unable to cast object of type 'Jayrock.Json.JsonObject' to type ...

580 views
Skip to first unread message

kimoy

unread,
Oct 9, 2008, 1:05:29 AM10/9/08
to Jayrock
hi,

i tried following the Person class example based on a previous (old)
post, but everytime i do an deserialization back to a Person object
the following error always gets displayed: "Unable to cast object of
type 'Jayrock.Json.JsonObject' to type Person".

this is my code:

public class Person
{
public string name;
public int age;
}

Person p1 = new Person();
p1.name = "john";
p1.age = 28;

string x = JsonConvert.ExportToString(p1);
Person p2 = (Person)JsonConvert.Import(x); // this line throws the
error

thanks.

Atif Aziz

unread,
Oct 9, 2008, 5:38:43 AM10/9/08
to jay...@googlegroups.com
> but everytime i do an deserialization back to a Person object
> the following error always gets displayed: "Unable to cast object of
> type 'Jayrock.Json.JsonObject' to type Person".

Do it this way, instead:

Person p2 = (Person) JsonConvert.Import(typeof(Person), x);

The overload without the type specification is precisely for the case where you don't have a custom type object to import into so it simply yields a dictionary to represent the JSON object.

kimoy

unread,
Oct 9, 2008, 6:24:04 AM10/9/08
to Jayrock
it works! thanks a lot.
Reply all
Reply to author
Forward
0 new messages