11

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.

6
  • 9
    I have tried : this,this,this,this,this,this and this <== this made my day, upvote for at least trying before asking a question Commented Sep 3, 2018 at 11:23
  • 1
    I think its the Guid maybe Commented Sep 3, 2018 at 11:26
  • 1
    Can you show us where you actually call the deserialization? Commented Sep 3, 2018 at 11:26
  • 1
    if you change CustID form Guid to String it will work for you with var m = JsonConvert.DeserializeObject<List<Project>>(json); Commented Sep 3, 2018 at 11:36
  • Is that the real JSON? Because that obviously ain't GUIDs. Commented Sep 3, 2018 at 12:00

2 Answers 2

8

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; }
    
Sign up to request clarification or add additional context in comments.

Comments

3

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.