Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the json response the property "data" is used as a List and in other places in the Json value is null.. How to include both scenario here when I am deserialzing and serializing the json.. without running in to exception A member with the name 'data' already exists. Use the JsonPropertyAttribute to specify another name

//Here is the Json
{
    "adRoots": null,
    "allowedTagTypes": [
        0,
        1,
        4
    ],
    "canEdit": true,
    "filterConfigData": {
        "config": [{
                "extraData": null,
                "id": "Endpoints.ID.SearchInProgress",
                "title": "Search in Progress",
                "type": 5
            },
            {
                "data": null,
                "extraData": null,
                "id": "Endpoints.ID.Policies",
                "source": {
                    "definition": "dbo",
                    "displayColumn": "Name",
                    "keyColumn": "Id",
                    "name": "Policies",
                    "type": 1
                },
                "title": "Policies",
                "type": 3
            },
            {
                "extraData": null,
                "id": "Endpoints.ID.MACs",
                "title": "MAC Addresses",
                "type": 1
            },
            {
                "data": [{
                        "name": "Endpoint Closed",
                        "value": 11
                    },
                    {
                        "name": "Endpoint Completed",
                        "value": 15
                    },
                    {
                        "name": "Endpoint Opened",
                        "value": 10
                    }

                ],
                "extraData": null,
                "id": "Endpoints.ID.ClientActivityState",
                "source": {
                    "definition": "State",
                    "displayColumn": "Name",
                    "keyColumn": "Id",
                    "name": "ClientActivityState",
                    "type": 2
                },
                "title": "Client Activity State",
                "type": 3
            },
            {
                "data": [{
                        "name": "System Alarm",
                        "value": 3
                    },
                    {
                        "name": "System Audit",
                        "value": 2
                    }
                ],
                "extraData": null,
                "id": "Endpoints.ID.AceType",
                "source": {
                    "definition": "AceType",
                    "displayColumn": "Name",
                    "keyColumn": "Id",
                    "name": "AceType",
                    "type": 2
                },
                "title": "ACL: ACE Type",
                "type": 3
            },
            {
                "extraData": null,
                "id": "Endpoints.ID.AceWho",
                "title": "ACL: Trustee",
                "type": 1
            },
            {
                "data": [{
                        "name": "Append Data",
                        "value": 4
                    },
                    {
                        "name": "Delete",
                        "value": 65536
                    },
                    {
                        "name": "Execute",
                        "value": 32
                    }

                ],
                "extraData": null,
                "id": "Endpoints.ID.AceRights",
                "source": {
                    "definition": "AceRights",
                    "displayColumn": "Name",
                    "editor": "ViewModel",
                    "keyColumn": "Id",
                    "name": "AceRights",
                    "type": 2
                },
                "title": "ACL: Authorization",
                "type": 3
            },
            {
                "extraData": null,
                "id": "Endpoints.ID.FilterTagName",
                "title": "Tag Name",
                "type": 1
            },
            {
                "data": null,
                "extraData": null,
                "id": "Endpoints.ID.FilterTags",
                "source": {
                    "definition": "dbo",
                    "displayColumn": "Name",
                    "keyColumn": "Id",
                    "name": "Tags",
                    "type": 1
                },
                "title": "Tags",
                "type": 3
            },
            {
                "extraData": null,
                "id": "Endpoints.Name.EndpointName",
                "title": "Endpoint Name",
                "type": 1
            },
            {
                "data": null,
                "extraData": null,
                "id": "Endpoints.Version.Version",
                "title": "Endpoint Version",
                "type": 1
            },
            {
                "extraData": null,
                "id": "Endpoints.Platform.Platform",
                "title": "Endpoint Platform",
                "type": 1
            },
            {
                "data": [{
                        "name": "Desktop",
                        "value": 1
                    },
                    {
                        "name": "Server",
                        "value": 2
                    },
                    {
                        "name": "Unknown",
                        "value": 0
                    }
                ],
                "extraData": null,
                "id": "ELPlatformType",
                "source": {
                    "displayColumn": "Name",
                    "keyColumn": "Id",
                    "name": "PlatformType",
                    "type": 2
                },
                "title": "Platform Type",
                "type": 3
            },
            {
                "extraData": null,
                "id": "Endpoints.LastPoll.LastPoll",
                "title": "Last Poll Time",
                "type": 2
            }

        ],
        "pagedListItems": null
    },
    "forests": null,
    "item": {
        "disallowed": false,
        "editPermissions": 0,
        "endpointsCount": 0,
        "filter": null,
        "id": 404,
        "ldapPaths": null,
        "name": "tag",
        "query": null,
        "type": 0
    }
}


What I have tried:

[JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)]
 public List<Datum> Data { get; set; }

[JsonProperty("data", NullValueHandling = NullValueHandling.Include)]
private List<Datum> Data2 { set { Data = value; } }
Posted
Updated 10-Jan-22 10:24am
Comments
Richard MacCutchan 22-Jul-20 4:09am    
You need to declare some method that handles the "data" objects and checks for null values.

1 solution

use [JsonIgnore] attribute on the field you want to ignore.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900