1

I'm quite new to Web API in C#.

This is my JSON response:

{
    "<chart_type>": [
        { "name": "entity" },
        { "display_name": "entity display name" },
        {
            "kpi": [
                {
                    "name":"<kpi>",
                    "display_name":"test",
                    "required": [
                        { "test": "test" },
                        { "test1": "test" }
                        ],
                    "optional": [        
                        { "test": "test" },
                        { "test": "test" }
                        ],
                    "objects": {
                        "<Fieldname>": 
                        {
                            "display_name":"<entity display name>",
                            "type": "<select or text>",
                            "default": "default value (already selected)",
                            "list": {
                                "<id>": "<value>"
                            }
                        }
                    }
                }
            ]
        }
    ]
}

but after I used postman this is the display:

API output

and this is my DefinitionDTO

public partial class Test
{
    [JsonProperty("<chart_type>", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public ChartType[] chart_type { get; set; }
}

public partial class ChartType
{
    [JsonProperty("name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string entity_name { get; set; }

    [JsonProperty("display_name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string entity_display_name { get; set; }

    [JsonProperty("kpi", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public Kpi[] Kpi { get; set; }
}

public partial class Kpi
{
    [JsonProperty("name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string kpi_name { get; set; }

    [JsonProperty("display_name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string kpi_display_name { get; set; }

    [JsonProperty("required", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public RequiredElement[] kpi_required { get; set; }

    //[JsonProperty("optional", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    //public Optional[] kpi_optional { get; set; }

    [JsonProperty("objects", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public Objects Objects { get; set; }
}

public partial class RequiredElement
{
    [JsonProperty("test", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string Test { get; set; }

    [JsonProperty("test1", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string Test1 { get; set; }
}

public partial class Objects
{
    [JsonProperty("<Fieldname>", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public Fieldname Fieldname { get; set; }
}

public partial class Fieldname
{
    [JsonProperty("display_name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string DisplayName { get; set; }

    [JsonProperty("type", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string Type { get; set; }

    [JsonProperty("default", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    public string Default { get; set; }

    //[JsonProperty("list", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
    //public List List { get; set; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

Use [DataContract] and [DataMember(EmitDefaultValue = false)] to your DTO's.

You have overriden default ASP.NET JSON serializer, If you're using the ASP.NET Web API, then you should be aware that the default JSON serializer isn't the DataContractJsonSerializer (DCJS), but JSON.NET instead. So unless you explicitly configure your JsonMediaTypeFormatter to use DCJS, you need another attribute to get the same behavior (JsonProperty, and its DefaultValueHandling property).

[DataContract]
public partial class Fieldname
{
[DataMember(EmitDefaultValue = false)]
[JsonProperty("display_name", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
public string DisplayName { get; set; }

[JsonProperty("type", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
public string Type { get; set; }

[JsonProperty("default", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
public string Default { get; set; }

//[JsonProperty("list", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
//public List List { get; set; }
}

For More to know please follow this Link

Rabby Hasan
  • 356
  • 4
  • 10
  • Hi sir, I try to change it into public class ChartType { public string kpi_name { get; set; } public string kpi_display_name { get; set; } public List kpi { get; set; } } public class RootObject { public List chart_type { get; set; } } when I call the RootObject it doesn't display the data first data, but when I try to call public class ChartType it will display the first two values and third one is null. I can't call the third one which is one of my problem. – Vince Neil Mapatac Pablo Jan 06 '20 at 06:41
  • [HttpGet] public HttpResponseMessage definition() { var result = _definitionRepository.definition(); if (result == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, " Bad request"); } return Request.CreateResponse(HttpStatusCode.OK, result); } – Vince Neil Mapatac Pablo Jan 06 '20 at 07:34
  • if possible, Need repository code also.. and add `DataMember` into all properties. – Rabby Hasan Jan 06 '20 at 09:49
  • public class DefinitionRepository : IDefinitionRepository { private HistogramEntities _db = new HistogramEntities(); public List definition() { var results = _db.Database.SqlQuery("get_definition_list").ToList(); return results; } } – Vince Neil Mapatac Pablo Jan 06 '20 at 21:08
0
public List<Root> definition()
        {
            var all = _db
                .kpi_definition
                .Include("KPI")
                .Select(dl => new Root
                {
                    //chart_type = dl.chart_type,
                    chart_type = new List<Chart>
                    {
                       new Chart { entity_name = dl.entity_name ,
                                   entity_display_name = dl.entity_display_name,
                                   kpi = new List<KPI>
                                   {
                                      new KPI {
                                               kpi_name = dl.kpi_name,
                                               kpi_display_name = dl.kpi_display_name,
                                               required = new List<Required>
                                               {
                                                   new Required
                                                   {
                                                       //kpi_required = dl.kpi_required
                                                   }
                                               },
                                               optional = new List<Optional>
                                               {
                                                   new Optional
                                                   {
                                                       //kpi_optional = dl.kpi_optional
                                                   }
                                               },
                                               objects = new List<Objects>
                                               {
                                                   new Objects
                                                   {
                                                       field_name = new List<FieldName>
                                                       {
                                                           new FieldName
                                                           {
                                                            entity_display_name = dl.entity_display_name,
                                                            type = "Select or Text",
                                                            @default = "default value(already selected)",
                                                            list = "",
                                                            ID = dl.ID
                                                           }
                                                       }

                                                   }
                                               }
                                              }
                                   }
                                 }
                    }
                }).ToList();
            return all;
        }