0

I'm consuming a rest api using Restsharp. The response of the api has this structure.

{
    "resourceId": "0014b07-92sl-si90",
    "property": [
        {
            "name": "prop1",
            "value": "-1.0"
        },
        {
            "name": "prop2",
            "value": "0.0"
        },
        {
            "name": "prop3",
            "value": "1000.0"
        },
        {
            "name": "prop4",
            "value": "Microsoft Windows"
        },
        {
            "name": "prop5",
            "value": "42917.0"
        }]
}

I want to deserialize this response to this domain model.

public class DomainModel 
{

    public double Prop1 {get; set;}

    public double Prop2 {get; set;}

    public double Prop3 {get; set;}

    public string Prop4 {get; set;}

}

As you can see I only need a subset of the JSON response properties and the response cannot be deserialized directly to a domain object.

Please how can I solve this issue.

Thank you.

Zack ISSOIR
  • 964
  • 11
  • 24
  • Please more describe it. How we can save string value in an integer variable. There is no problem with losing the data? If you show a sample can be helpful. – Saeid Amini Jun 26 '19 at 09:56
  • Take a look a this answer. You probably need a custom deserialization: https://stackoverflow.com/questions/41510242/custom-deserializer-only-for-some-fields-with-json-net – ste-fu Jun 26 '19 at 10:01
  • @SaeidAmini it's not a problem I can make all the properties string properties. The problem is how can I deserialize them to that domain model. – Zack ISSOIR Jun 26 '19 at 10:07

3 Answers3

2
public class DomainModel
{
    public string Prop1 { get; set; }

    public string Prop2 { get; set; }

    public string Prop3 { get; set; }

    public string Prop4 { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string json = @"{
                        ""resourceId"": ""0014b07 - 92sl - si90"",
                        ""property"": [
                            {
                                ""name"": ""prop1"",
                                ""value"": ""-1.0""
                            },
                            {
                                ""name"": ""prop2"",
                                ""value"": ""0.0""
                            },
                            {
                                ""name"": ""prop3"",
                                ""value"": ""1000.0""
                            },
                            {
                                ""name"": ""prop4"",
                                ""value"": ""Microsoft Windows""
                            },
                            {
                                ""name"": ""prop5"",
                                ""value"": ""42917.0""
                            }]
                    }";
        var parsedJason = JObject.Parse(json);

        DomainModel result = new DomainModel();
        var jsonValues = parsedJason["property"].Select(x => ((JObject)x)).ToList();

        var props = typeof(DomainModel).GetProperties();
        jsonValues.ForEach(x =>
        {
            var jsonPropName = x.GetValue("name").ToString();
            var jsonPropValue = x.GetValue("value").ToString();

            var prop = props.Where(p => p.Name.ToUpper() == jsonPropName.Trim().ToUpper()).FirstOrDefault();
            if (prop != null)
                prop.SetValue(result, jsonPropValue, null);
        });
    }
}

enter image description here

Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
1

I would use Json.NET and use the JsonProperty attribute as described in this answer: .NET NewtonSoft JSON deserialize map to a different property name

James Harcourt
  • 6,017
  • 4
  • 22
  • 42
  • 1
    I'm not sure the JsonProperty will deal with changing the types or data structure – ste-fu Jun 26 '19 at 09:56
  • Giving that all objects is the property array come with the structure {"name" : "prop-name", "value" : "prop-value" } , I dont think JsonProperty is useful here. Can you edit the response to include an example for my json response ? – Zack ISSOIR Jun 26 '19 at 10:11
1

You can either deserialize the response to this class:

public class MyResponse
{
    public string resourceId {get;set;}
    public List<MyProperty> property {get;set;}
}

public class MyProperty
{
    public string name {get;set;}
    public string value {get;set;}
}

Then convert MyResponse to DomainModel:

DomainModel myDomainModel = new DomainModel();
myDomainModel.Prop1 = myResponse.property.Where(c=>c.name =="prop1").SingleOrDefault()?.value;
myDomainModel.Prop2 = myResponse.property.Where(c=>c.name =="prop3").SingleOrDefault()?.value;
myDomainModel.Prop2 = myResponse.property.Where(c=>c.name =="prop3").SingleOrDefault()?.value;
myDomainModel.Prop4 = myResponse.property.Where(c=>c.name =="prop4").SingleOrDefault()?.value;

Or build your logic into a custom deserializer.

Elias N
  • 1,430
  • 11
  • 19
  • I'm aware of this solution, but I cannot use it , because It will not be efficient in my case. Could you please edit the response with a custom Json deserializer, – Zack ISSOIR Jun 26 '19 at 10:15