Error formatting the Json

Asked

Viewed 36 times

1

I need to generate and expose a Json via web api and the doubt is as follows:
1st. - You cannot expose the attribute "services":null, as the Json generated by me described below ?
2nd. - How do I generate at the beginning of Json: "services": [ as the Requested Json ?

Models:

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; }

}

What JSON should look like:

{
    "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
                }
    }
]

1 answer

0

Whoa, come on, come on.

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

example, in Jackson json you can do this in 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, he will be created.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.