C# 在 JSON 反序列化期间没有为“System.String”类型定义无参数构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9386929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:09:31  来源:igfitidea点击:

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

c#jsondeserialization

提问by Justin R.

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

这看起来应该很简单,但是当我尝试一些简单的 JSON反序列化为托管类型时出现异常。例外是:

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

MissingMethodException
没有为“System.String”类型定义无参数构造函数

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

虽然 System.String 确实没有无参数构造函数,但我不清楚为什么这很重要。

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:

这是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:

即使我的 JSON 只是:

{
    "id": "uniquestring"
}

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

谁能告诉我为什么 System.String 需要无参数构造函数?

采纳答案by Sergey Sirotkin

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. Instantiatethe 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.
  1. 从输入流中获取一种对象(在本例中为字符串)
  2. 实例化对象。如果没有默认构造函数,您就无法做到这一点
  3. 从流中读取属性/值
  4. 将流中的值分配给在步骤 2 中创建的对象。

回答by Amer Diglisic

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);