Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

NullReferenceException: Object reference not set to an instance of an object How to foreach deserializeobject a json list of objects in c#?

$
0
0

i am  facing problems in iterating through my Deserialized json response.

My JSON and classes structure are stated below. The code I am using to iterate is as follows:

 public class CONISRootObject
    {
        public CONISSubObject[] Properties { get; set; }
    }

    public class CONISSubObject
    {
        public string predicate { get; set; }
        public string objectType { get; set; }
        public ConsentData[] consentData { get; set; }
    }

    public class ConsentData
    {
        public string key { get; set; }
        public string value { get; set; }
    }
[
    {"predicate": "AGREES_COMMUNICATE","objectType": "PersonalData","consentData": [
            {"key": "objCreatedBy","value": "IMKRY"
            },
            {"key": "mailAddress","value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {"key": "objCreatedAt","value": "2018-02-07 14:58:43"
            }
        ]
    },
    {"predicate": "AGREES_STORAGE","objectType": "PersonalData","consentData": [
            {"key": "objCreatedBy","value": "IMKRY"
            },
            {"key": "mailAddress","value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {"key": "objCreatedAt","value": "2018-02-07 14:58:43"
            }
        ]
    },
    {"predicate": "HAS_RAWDATA","objectType": "RawDemoMail1Consent","consentData": [
            {"key": "PersonalID","value": "0012400000ayGEaAAM"
            },
            {"key": "objCreatedBy","value": "IMKRY"
            },
            {"key": "MailAddress","value": "0b6cbeb86e42c6191671b34b7c9808e31c9e5f5acaecaf9ee4cfbd379abcdba9"
            },
            {"key": "GivenDate","value": "2017-11-02"
            },
            {"key": "Domain","value": "aspirin.com"
            },
            {"key": "ConsentID","value": "aWe7-12373"
            },
            {"key": "objCreatedAt","value": "2018-02-07 14:33:33"
            },
            {"key": "SourceSystem","value": "VEEVA_FR"
            }
        ]
    },
    {"predicate": "IS_VALID_FOR","objectType": "Context","consentData": [
            {"key": "objCreatedBy","value": "IMKRY"
            },
            {"key": "webDomain","value": "aspirin.com"
            },
            {"key": "objCreatedAt","value": "2018-02-06 15:05:00"
            }
        ]
    },
    {"predicate": "IS_VALID_TO_DATE","objectType": "TimeTree,Day","consentData": [
            {"key": "objCreatedBy","value": "imkry"
            },
            {"key": "dateStr","value": "2018-11-01"
            },
            {"key": "month","value": "11"
            },
            {"key": "year","value": "2018"
            },
            {"key": "objCreatedAt","value": "2018-02-05 14:44:09"
            },
            {"key": "day","value": "1"
            }
        ]
    },
    {"predicate": "IS_VALID_FROM_DATE","objectType": "TimeTree,Day","consentData": [
            {"key": "objCreatedBy","value": "imkry"
            },
            {"key": "dateStr","value": "2017-11-02"
            },
            {"key": "month","value": "11"
            },
            {"key": "year","value": "2017"
            },
            {"key": "objCreatedAt","value": "2018-02-05 14:44:09"
            },
            {"key": "day","value": "2"
            }
        ]
    }
]
   var CONISRootObjectList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CONISRootObject>> (conisResponse);

                foreach (CONISRootObject cRO in CONISRootObjectList)
                {
                    foreach (CONISSubObject cSO in cRO.Properties)
                    {

                        Console.WriteLine($" Predicate : " + cSO.predicate + " Object Type : " + cSO.objectType);

                        foreach (ConsentData cData in cSO.consentData)
                        {
                            Console.WriteLine($" Key : " + cData.key + " Value : " + cData.value);

                        }
                    }
                }

But I get this error: NullReferenceException: Object reference not set to an instance of an object.

at: ...foreach (CONISSubObject cSO in cRO.Properties)...

Any hint why is that the case? Thanks.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>