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.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
  • 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 – TheGeneral Sep 03 '18 at 11:23
  • 1
    I think its the Guid maybe – TheGeneral Sep 03 '18 at 11:26
  • 1
    Can you show us where you actually call the deserialization? – Patrick Hofman Sep 03 '18 at 11:26
  • 1
    if you change CustID form Guid to String it will work for you with `var m = JsonConvert.DeserializeObject>(json);` – styx Sep 03 '18 at 11:36
  • Is that the *real* JSON? Because that obviously ain't GUIDs. – nvoigt Sep 03 '18 at 12:00
  • Here is another strange thing that i came across while experimenting. when using var json = await httpResponse.Content.ReadAsStringAsync(); and trying to deserialize with return JsonConvert.DeserializeObject>(json); it gives me the same error. if instead i debug the string in the json variable and deserialize with return JsonConvert.DeserializeObject>("string here"); it works even if you dont tinker with Guid ^_^ – whitefrog0110 Sep 03 '18 at 14:10

2 Answers2

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; }
    
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
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 .

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
sagi
  • 40,026
  • 6
  • 59
  • 84