2

Hi Request your help in Below Scenario. I have a Requirement where in I want to return Response JSON from DotNet WEB API As below.

{ "Status":"OK", "Series":[["GREEN",40.5],["RED",12.8],["YELLOW",12.8]] }

Below are my Code Sample and Output.

public class Response
{
public string Status {get;set;}
public List<SampleData> Series {get;set;}
}

public class SampleData
{
public string ColorCode{get;set;}
public double ColorVal{get;set;}
}

API Get Method

public Response Get()
        {
            Response Objresponse = new Response();
             Objresponse.Status ="OK";
             List<SampleData> _sampleList = new List<SampleData>();
             _sampleList.Add(new SampleData{ ColorCode="GREEN", ColorVal=40.5});
             _sampleList.Add(new SampleData{ ColorCode="RED",ColorVal=12.8});
             _sampleList.Add(new SampleData{ ColorCode="YELLOW",ColorVal=12.8});
             Objresponse.Series = _sampleList;
            return Objresponse;
        }

JSON Output which is returned

{
"Status":"OK",
"Series":[
{"ColorCode":"GREEN","ColorVal":40.5},
{"ColorCode":"RED","ColorVal":12.8},
{"ColorCode":"YELLOW","ColorVal":12.8}
]
}

Request your help to return the output as per my requirement. Thank you.......

Akos Nagy
  • 4,201
  • 1
  • 20
  • 37
  • 1
    You can either change the List into a Dictionary or a list. That way it should serialize the way you want it to. If you don't want to do that look up how to implement a `JsonConverter` create one for the class type `Response` and make sure the server globally adds this as one of Json.Net Converters – Arno Nov 30 '17 at 08:56

1 Answers1

2

Probably the easiest way would be to create a custom JsonConverter and transform your response inside to your required format:

public class ResponseConverter : JsonConverter
{
    public override bool CanConvert(Type objectType) => objectType == typeof(Response);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotImplementedException();

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        Response originalResponse = (Response)value;
        writer.WriteStartObject();
        writer.WritePropertyName(nameof(originalResponse.Status));
        writer.WriteValue(originalResponse.Status);
        writer.WritePropertyName(nameof(originalResponse.Series));
        writer.WriteStartArray();            
        foreach (var val in originalResponse.Series)
        {
            writer.WriteStartArray();
            writer.WriteValue(val.ColorCode);
            writer.WriteValue(val.ColorVal);
            writer.WriteEndArray();
        }
        writer.WriteEndArray();
        writer.WriteEndObject();
    }
}

Then, you can decorate your Response class with a special attribute to denote that it should be serialized with this converter:

[JsonConverter(typeof(ResponseConverter))]
public class Response
{
    public string Status { get; set; }
    public List<SampleData> Series { get; set; }
}
Akos Nagy
  • 4,201
  • 1
  • 20
  • 37
  • 1
    `Then, you have to decorate your Response class` there also is the option of adding the converter globally. As Shown [here](https://stackoverflow.com/a/19512471/8579225) – Arno Nov 30 '17 at 09:08
  • @Drag0nvil True, that's another possible option. – Akos Nagy Nov 30 '17 at 09:08
  • Hi Thank you for your response. I am getting below response after trying the solution suggested. { "Status": "OK", "Series": { "GREEN": 40.5, "RED": 12.8, "YELLOW": 12.8 } } by any chance is it possible to make series value like this "Series":[["GREEN",40.5],["RED",12.8],["YELLOW",12.8]] – Rohit Naik Sawal Nov 30 '17 at 09:46
  • Sorry for that, I missed the square brackets (it's still early for me here :) ).So basically you want an array of arrays, where the inner arrays have a string and a float for elements. I have to say, it's a little strange, but I'm not the one to judge. See my edited answer, hopefully it works as required now. – Akos Nagy Nov 30 '17 at 10:02