Error formatting Json

0

I need to generate and expose a Json via web api and the question is this:
1o. - Can not expose the attribute "services":null , according to the JSon generated by me described below? Home 2nd. - How do I generate at the start of Json: "services": [ as requested by Json?

Templates:

public class ProductType
{
    public ProductType()
    {
        services = new Services();
    }
    public string productType { get; set; }
    public Services services { get; set; }
}

public class Services
{
    public string subtype { get; set; }
    public string services { get; set; }

}

How should JSON be:

{
    "services": [
        {
            "productType": "Produto A",            
                "services": [
                    {
                        "subtype": "Sub Produto A1"
                    }               
                ]
        },
        {
            "productType": "Produto B",
                "services": [
                    {
                        "subtype": "Sub Produto A2"
                    }
                ]
        },
        {
            "productType": "Produto C",
                "services": [
                    {
                        "subtype": "Sub Produto A3"
                    }
                ]
        }        
    ]
}

How I am generating JSON:

[
    {
        "productType":"Produto A",
        "services":
                {
                    "subtype":"Sub Produto A",
                    "services":null
                }
    },
    {
        "productType":"Produto B",
        "services":
                {
                    "subtype":"Sub Produto B",
                    "services":null
                }
    },
    {
        "productType":"Produto C",
        "services":
                {
                    "subtype":"Sub Produto C",
                    "services":null
                }
    }
]
    
asked by anonymous 17.08.2018 / 01:57

1 answer

0

Oops, come on.

You can actually hide, look in the API you are using which method or annotation you ignore nulls.

For example, in jackson json you can do this on mapper

mapper.setSerializationInclusion (Include.NON_NULL);

When the services label appears, you can create a JSON Staff object that contains an object of type services. With this, it will be created.

    
29.08.2018 / 04:24