简体   繁体   中英

No parameterless constructor defined for type of 'System.String' during JSON deserialization

This seems like it should be so easy, but I am getting an exception when I try to deserialize some straightforward JSON into a managed type. The exception is:

MissingMethodException
No parameterless constructor defined for type of 'System.String'

While it is true that there are no parameterless constructors for System.String, I'm not clear as to why this matters.

The code that performs the deserialization is:

using System.Web.Script.Serialization;
private static JavaScriptSerializer serializer = new JavaScriptSerializer();
public static MyType Deserialize(string json)
{
    return serializer.Deserialize<MyType>(json);
}

My type is roughly:

public class MyType
{
    public string id { get; set; }
    public string type { get; set; }
    public List<Double> location { get; set; }
    public Address address { get; set; }
    public Dictionary<string, string> localizedStrings { get; set; }
}

The other class is for an address:

public class Address
{
    public string addressLine { get; set; }
    public string suite { get; set; }
    public string locality { get; set; }
    public string subdivisionCode { get; set; }
    public string postalCode { get; set; }
    public string countryRegionCode { get; set; }
    public string countryRegion { get; set; }
}

Here's the JSON:

{
    "id": "uniqueString",
    "type": "Foo",
    "location": [
        47.6,
        -122.3321
    ]
    "address": {
        "addressLine": "1000 Fourth Ave",
        "suite": "en-us",
        "locality": "Seattle",
        "subdivisionCode": "WA",
        "postalCode": "98104",
        "countryRegionCode": "US",
        "countryRegion": "United States"
    },
    "localizedStrings": {
        "en-us": "Library",
        "en-ES": "La Biblioteca"
    }
}

I get the same exception even if my JSON is just:

{
    "id": "uniquestring"
}

Can anybody tell me why a parameterless constructor is needed for System.String?

Parameterless constructors need for any kind of deserialization. Imagine that you are implementing a deserializer. You need to:

  1. Get a type of object from the input stream (in this case it's string)
  2. Instantiate the object. You have no way to do that if there is no default constructor .
  3. Read the properties/value from stream
  4. Assign the values from the stream to the object created on step 2.

I had the same issue and this was what fixed the issue.

Cheers!

//Deserializing Json object from string
DataContractJsonSerializer jsonObjectPersonInfo = 
    new DataContractJsonSerializer(typeof(PersonModel));
MemoryStream stream = 
    new MemoryStream(Encoding.UTF8.GetBytes(userInfo));
PersonModel personInfoModel = 
    (PersonModel)jsonObjectPersonInfo.ReadObject(stream);

Resurrecting this in case I need it myself; in my case the error was in the JSON.

I was deserializing to a List<string> but the JSON was erroneously passing a list of ey value pairs eg

{
    "Roles": [ 
        { "ID": "Administrator", "Name": "Administrator" }, 
        { "ID": "Client", "Name": "Client" }
    ]
}

Into:

public List<string> Roles { get; set; }

This gives the OP's error when it tries to deserialize the objects into strings.

It'd be handy if if gave the property name it failed on!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM