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.......

1
  • 1
    You can either change the List<SampleData> into a Dictionary<string, double> or a list<string, double>. 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 Commented Nov 30, 2017 at 8:56

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Then, you have to decorate your Response class there also is the option of adding the converter globally. As Shown here
@Drag0nvil True, that's another possible option.
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]]
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.