Search code examples
c#jsonserializationjavascriptserializer

"[]" is an invalid JSON primitive?


I get the error Invalid JSON primitive. So I keep simplifying my JSON string, right down to the empty array: [] I even checked that the string looks right, and has length 2.

I do believe that [] should be valid for an array of anything?

My code is

jsonString = "[]";
var v = new JavaScriptSerializer();
Felagi[] felagar = (Felagi[])v.Deserialize<Felagi[]>("jsonString");

and for what it may be worth, the class I try to deserialize is

public class Felagi
{
    public String firstName { get; set; }
}

Solution

  • You are trying to deserialize the string literal "jsonstring", not the contents of variable jsonString. Try

    Felagi[] felagar = (Felagi[])v.Deserialize<Felagi[]>(jsonString);