I have a json as below :
"[{"a":"b","c":"d"},{"a":"e","c":"f"},{"a":"g","c":"h"}]"
now I want to deserilize this into a list of objects of anonymous type "foo"
var foo=new { a=string.empty , c=string.empty };
the code is :
ServiceStackJsonSerializer Jserializer = new ServiceStackJsonSerializer();
dynamic foos = Jserializer.Deserialize<List<foo.GetType()>>(jsonString);
but not working .
update :
replacing ServiceStack
with JavascriptSerializer
and passing dictionary[]
solved the problem without need to anonymous
Type
JavaScriptSerializer jSerializer = new JavaScriptSerializer();
var Foos = jSerializer.Deserialize<Dictionary<string, object>[]>(jsonString);
I don't know what the Jserializer
class is, but I do know of the JavaScriptSerializer
class. Unfortunately, it doesn't support deserialization into anonymous types. You'll have to create a concrete type like this:
class Foo
{
public string a { get; set; }
public string c { get; set; }
}
Using the following code worked for me:
const string json =
@"[{""a"":""b"",""c"":""d""},{""a"":""e"",""c"":""f""},{""a"":""g"",""c"":""h""}]";
var foos = new JavaScriptSerializer().Deserialize<Foo[]>(json);
the variable foos
will contain an array of Foo
instances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With