1

I have a string of JSON that I'm trying to convert into a list. There is an empty array value which is breaking the deserialization (have tried removing it manually or changing to a number and it works). Was thinking I could try to replace the [] in the string, but is there a better way to work around this?

public class Tax
{
    public string Id;
    public string Name;
    public string PathOfTerm;
    public string Children;
    public string Level;
    public string RawTerm;
}

var exString = "[{\"Id\":\"12345\",\"Name\":\"aName\",\"PathOfTerm\":\"aTerm\",\"Children\":[],\"Level\":0,\"RawTerm\":null}]";
JavaScriptSerializer ser = new JavaScriptSerializer();
var taxData = ser.Deserialize<List<Tax>>(exString);
3
  • 3
    Have you tried making your model reflect your JSON instead? It looks like Children should be an array (can't tell what kind here) rather than a string... Commented May 25, 2017 at 16:20
  • Yep thanks that was it. Didn't realize that couldn't be converted to a string Commented May 25, 2017 at 16:25
  • Okay, will post as an answer. Commented May 25, 2017 at 16:28

1 Answer 1

7

The value is an array, but your field is a string. I suggest you make it an array (or list) of the appropriate type - we can't tell what that type would be from your JSON, but perhaps you want a string array?

I'd also suggest using properties instead of public fields.

(If you can move to Json.NET, I'd generally recommend that over JavaScriptSerializer, too...)

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.