I'm receiving from an API a result that is something like:
[{
"propID": 1,
"propname": "nameA",
"dataType": "N",
"value": "9"
},
{
"propID": 2,
"propname": "nameB",
"dataType": "VL",
"value": "dasdsa"
},
{
"propID": 3,
"propname": "nameC",
"dataType": "N",
"value": "7"
},
{
"propID": 4,
"propname": "nameD",
"dataType": "VL",
"value": "jmfidsnjfs"
}
]
I'm getting this and decoding this into an DTO so I can convert the numeric values into numerics. My DTO looks like:
public class PropertyToInsertDto
{
[JsonIgnore]
public int propID { get; set; }
public string propname { get; set; }
[JsonIgnore]
public string dataType { get; set; }
[JsonIgnore]
public string value { get; set; }
public string valueString { get; set; }
public float valueInt { get; set; }
}
So, imagining I store the API into string variable called result I would decode this using
var properties = JsonConvert.DeserializeObject<List<PropertyToInsertDto>>(result);
and then iterating each property to convert into numeric values
foreach(var property in properties) {
if (string.IsNullOrEmpty(property.value))
continue;
if (property.dataType == "N") {
property.valueInt = float.Parse(property.value);
} else {
property.valueString = property.value;
}
}
I want to convert this into Json so the result is
{"nameA": 9, "nameB":"dasdsa", "nameC":7, "nameD": "jmfidsnjfs"}
I tried using the SerializeObject method from JsonConvert without any good result. My biggest problem is due to the fact that the result can come from valueInt or valueString depending if it is a number or a text.
Thanks!
Kuno