简体   繁体   中英

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 :

   [{
        "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 :

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'.

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.

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 .

You can use a JsonConverter for converting string to Guid on deserialization (available in 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 )
  • Apply [JsonConverter(typeof(GuidJsonConverter))] to the CustID property

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

Use string instead of 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 .

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