1

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

  • 1
    Your *actual* problem is how to convert one object into another, not how to serialize JSON. A quick&dirty fix would be to create a dictionary of names/values, and serialize it. A `JObject` on the other hand is essentially a dictionary so you can add attributes to it directly – Panagiotis Kanavos May 20 '19 at 07:34

2 Answers2

3

First of all you ignored "value" property, so this property isn't deserialized by JsonConvert and always has default value.

[JsonIgnore]
public string value { get; set; }

"valueString" and "valueInt" aren't required in this DTO, you need separated DTOs to read and write because you are changing object structure.

You can get expected result using this code:

var properties = JsonConvert.DeserializeObject<List<PropertyToInsertDto>>(str);
var result = JsonConvert.SerializeObject(properties.ToDictionary(
        x => x.propname, 
        x => x.dataType == "N" ? (object)float.Parse(x.value) : x.value));
0

You can create a dictionary and then convert it to a json like this:

https://www.newtonsoft.com/json/help/html/SerializeDictionary.htm

Instead of int type as the value you can use object. Or use a string even for number types, but you would have to use custom convert type when deserializing in the future operations.

This might help as well:

Serializing/Deserializing Dictionary of objects with JSON.NET

vsarunov
  • 1,433
  • 14
  • 26