1

I have JSON file coming from 3rd party systems where all "string", "double" and "Boolean" comes as double quote as string data.

 [{"Key":"Key1","Value":"Value1"},{"Key":"Key2","Value":"80"},{"Key":"Key3","Value":"true"}]

I need to convert accordingly to double, boolean and string, I have defined a class like, value as "object"

 public class Data
{
    public string Key { get; set; }
    public object Value { get; set; }
}

and deserialize the data like,

var data = JsonConvert.DeserializeObject<List<Data>>(File.ReadAllText(@"C:\TEMP\data.json"));

but the value is coming in double quote as this treats as string ,

enter image description here enter image description here

How to do the conversion so that double and boolean value comes without double quote,

enter image description here

user584018
  • 10,186
  • 15
  • 74
  • 160
  • 1
    You can use online tool (e.g. [this one](https://json2csharp.com/)) to convert json to classes. In given json all values are strings (forget about double, bools, etc). Deserialize them as string and those what you think are doubles [convert to double](https://stackoverflow.com/q/11399439/1997232) (the good question is how do you know what are double, perhaps by using `TryParse` method and if it works). – Sinatr Dec 08 '20 at 10:34
  • can't you just cast them into bool? – Gilad Dec 08 '20 at 10:35
  • @Gilad, you can't cast string to bool, you need to either parse or [convert](https://stackoverflow.com/a/20893956/1997232). – Sinatr Dec 08 '20 at 10:37
  • Use _"Edit -> Paste Special -> Paste JSON As Classes"_, which will generate appropriate c# code for deserializing your JSON string. – SᴇM Dec 08 '20 at 10:39
  • 1
    You might want to consider deserialising the data to a Dictionary object rather than your own 'Data' class. Something like this: `var data = JsonConvert.DeserializeObject>>(File.ReadAllText(@"C:\TEMP\data.json"));` – PeterG Dec 08 '20 at 10:44
  • @PeterG. could you show some code please? – user584018 Dec 08 '20 at 10:47

1 Answers1

3

@user584018 you can write your JsonConverters, there are many ways you could, I taken the code from post and modified based on your criteria. JsonConverters and object properties

public class PolymorphicConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(object);
    }

    public override bool CanWrite { get { return false; } }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader);
        if (double.TryParse(token.ToString(), out double doubleValue))
            return doubleValue;
        if (bool.TryParse(token.ToString(), out bool boolValue))
            return boolValue;
        return token.ToString();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    } 

}

public class Data
{
    public string Key { get; set; }
    [JsonConverter(typeof(PolymorphicConverter))]
    public object Value { get; set; }
}
var data = JsonConvert.DeserializeObject<List<Data>>(File.ReadAllText(@"C:\TEMP\data.json"));
coder_b
  • 827
  • 6
  • 15