简体   繁体   English

无法将类型为“ System.String”的对象转换为类型为“ System.Collections.Generic.List`1”

[英]Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1

I am trying to deserializing json string to a complex object with no success: 我试图将json字符串反序列化为复杂对象,但没有成功:

This is the class I am trying to deserialize to: 这是我要反序列化的课程:

public class ExcludePublisherRule : BaseAutomaticRule
{

    public int LineIdx { get; set; }

    [Required]
    [Range(30, 1000)]
    public int MininumInstalls { get; set; }

    [Required]
    public int UsingDataFrom { get; set; }

    [Required]
    public List<PostEventModel> PostEventsModels { get; set; }

}

public abstract class BaseAutomaticRule
{
    public int Id { get; set; }

    [Required(ErrorMessage = "*Rule name is required")]
    [StringLength(70)]
    public string Name { get; set; }

    public DateTime LastActivated { get; set; }

    [Required]
    public string CampaignId { get; set; }

    public int StatusId { get; set; }
}


public class PostEventModel
{
    public int Id { get; set; }
    public int PublisherInstalls { get; set; }
}


This is how I try to do it:
//Get type and Object and returns a class object.
    public T ConvertToAutomaticRule<T>(dynamic automaticRuleJSON)
    {
        var json = "";
        try
        {
            var serializer = new JavaScriptSerializer();
            json = serializer.Serialize(automaticRuleJSON);
            return serializer.Deserialize<T>(json);
        }
        catch (Exception ex)
        {
            log.Error($"Ex message: {ex.Message}, json is {json}");
            return default(T);
        }

    }

The json: json:

{"automaticRuleName":"asd","installsNumber":"30","usingDataFrom":"1","ruleStatusId":"1","automaticRuleID":"0","PostEventsModels":"[{\"Id\":\"23\",\"PublisherInstalls\":\"15\"},{\"Id\":\"2\",\"PublisherInstalls\":\"96\"}]","campaignId":"95e62b67-ba16-4f76-97e4-dd96f6e951c7"}

But I keep getting the error above, what's wrong with this way? 但是我总是遇到上述错误,这种方式有什么问题?

The JSON explicitly says it is a string, not an array; JSON明确表示它是一个字符串,而不是数组。

"PostEventsModels":"[{\"Id\":\"23\",...

should be: 应该:

"PostEventsModels":[{"Id":23,...

Fix the source JSON 修复源JSON

If you format your json (for example on https://jsonformatter.curiousconcept.com/ ) you can see that the property PostEventsModels is not a json list, but a string representation of it. 如果格式化json(例如,在https://jsonformatter.curiousconcept.com/上 ),则可以看到属性PostEventsModels不是json列表,而是它的字符串表示形式。

{  
   "automaticRuleName":"asd",
   "installsNumber":"30",
   "usingDataFrom":"1",
   "ruleStatusId":"1",
   "automaticRuleID":"0",
   "PostEventsModels":"[{\"Id\":\"23\",\"PublisherInstalls\":\"15\"},{\"Id\":\"2\",\"PublisherInstalls\":\"96\"}]",
   "campaignId":"95e62b67-ba16-4f76-97e4-dd96f6e951c7"
}

So you need to correct the json generation, or let property PostEventsModels be a string, and then deserialize this string later. 因此,您需要更正json的生成,或让属性PostEventsModels为字符串,然后稍后反序列化此字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法反序列化当前JSON对象..以键入&#39;System.Collections.Generic.List`1 [System.String]&#39; - Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]' asp.net json Web 服务发送数组错误无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - asp.net json web service sending array error Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法将类型&#39;object [] []&#39;转换为&#39;System.Collections.Generic.List <object> “ - Cannot convert type 'object[][]' to 'System.Collections.Generic.List<object>' 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' 无法隐式转换类型&#39;System.Collections.Generic.List <System.Data.DataRow> &#39;到&#39;System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.List &lt; <anonymous type> &gt;”到“ System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type>>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM