简体   繁体   English

从json转换为List <object> 造成例外

[英]Converting from json to List<object> causing exception

So here is my problem, I have an API setup that returns results from Azure Storage Table in JSON string format : 所以这是我的问题,我有一个API设置,以JSON字符串格式返回Azure存储表的结果:

   [{
        "CustID": "f3b6.....0768bec",
        "Title": "Timesheet",
        "CalendarID": "AAMkADE5ZDViNmIyLWU3N2.....pVolcdmAABY3IuJAAA=",
        "PartitionKey": "Project",
        "RowKey": "94a6.....29a4f34",
        "Timestamp": "2018-09-02T11:24:57.1838388+03:00",
        "ETag": "W/\"datetime'2018-09-02T08%3A24%3A57.1838388Z'\""
    }, {
        "CustID": "5479b.....176643c",
        "Title": "Galaxy",
        "CalendarID": "AAMkADE5Z.......boA_pVolcdmAABZ8biCAAA=",
        "PartitionKey": "Project",
        "RowKey": "f5cc....86a4b",
        "Timestamp": "2018-09-03T13:02:27.642082+03:00",
        "ETag": "W/\"datetime'2018-09-03T10%3A02%3A27.642082Z'\""
    }]

And I am trying to convert it back to Project object : 我试图将其转换回Project对象:

public class Project : TableEntity
    {
        public Project() { }

        public Project(string rKey, string pKey = "Project")
        {
            this.PartitionKey = pKey;

            this.RowKey = rKey;
        }

        public Guid CustID { get; set; }
        public string Title { get; set; }
        public string CalendarID { get; set; }
    }

I keep getting "Error converting value from 'string' to 'object'. 我一直收到“将值从'string'转换为'object'时出错。

I have tried : this , this , this , this , this , this and this and it won't work. 我曾尝试: 这个这个这个这个这个这个这个 ,它不会工作。 Always the same error. 总是一样的错误。 It's obvious I am missing something, but it has been two days and I can't seem to figure it out. 很明显我错过了一些东西,但已经有两天了,我似乎无法弄明白。 Have tried modifying the object class to include all fields, tried adding another class that has list property, even tried passing it as Array. 尝试修改对象类以包含所有字段,尝试添加另一个具有list属性的类,甚至尝试将其作为Array传递。

At this point, I would be grateful of any kind of help. 在这一点上,我将感激任何形式的帮助。

The problem is that you have a string CustID which can't be deserialized into Guid . 问题是你有一个字符串CustID ,无法反序列化为Guid

You can use a JsonConverter for converting string to Guid on deserialization (available in JSON.NET ): 您可以使用JsonConverter在反序列化时将string转换为Guid (在JSON.NET可用):

public class GuidJsonConverter : JsonConverter<Guid>
{
    public override void WriteJson(JsonWriter writer, Guid value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }

    public override Guid ReadJson(JsonReader reader, Type objectType, Guid existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        string s = (string)reader.Value;

        return new Guid(s);
    }
}

Two way to use it: 两种方式使用它:

  • Pass converter instance to DeserializeObject method (example here ) 将转换器实例传递给DeserializeObject方法( 此处示例)
  • Apply [JsonConverter(typeof(GuidJsonConverter))] to the CustID property [JsonConverter(typeof(GuidJsonConverter))]应用于CustID属性

     [JsonConverter(typeof(GuidJsonConverter))] public Guid CustID { get; set; } 

Use string instead of Guid : 使用string而不是Guid

 public string CustID { get; set; }

As the error states : Error converting value string to object , the only object you got there is Guid which probably have a problem desirializing a string in it . 正如错误所述: Error converting value string to object ,您在那里找到的唯一对象是Guid ,它可能会在其中使用字符串时出现问题。

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

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