0

I am trying to deserialize the json string to a c# object.

string str ="[{ \"foo\" : \"A\" , \"bar\" : \"B\"}, { \"foo\" : \"C\" , \"bar\" : \"D\"}]";

public Class Example
{
  public string foo { get; set; }
  public string bar { get; set; }
}

JavaScriptSerializer Js = new JavaScriptSerializer();
Example[] ex = (Example[]) Js.DeserializeObject(str);

But I am getting an InvalidCast Exception. What am I doing wrong?

1
  • str is a C# array, not a Json array, try string str = "[{ \"foo\" : \"A\" , \"bar\" : \"B\"}, { \"foo\" : \"C\" , \"bar\" : \"D\"}]"; Commented Sep 1, 2012 at 19:42

1 Answer 1

7
var list = new JavaScriptSerializer().Deserialize<List<FooBar>>(str);

public class FooBar
{
    public string foo;
    public string bar;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.