Skip to content

how to check if a key exists in a json file and if that key does exist, then add that key to a dictionary? #123

@corbinyo

Description

@corbinyo

How can I check if the key (int)myJsonData[0]["listings"][i]["names"]["firstName"] exists in my json file (which is pulled from server)? I use myJsonData = JsonMapper.ToObject("[" + webRequest.downloadHandler.text.Trim() + "]"); to handle the Json data
and then I use

myJsonData.Add(new myClass( (int)itemData[0]["listings"][i]["names"]["firstName"]

to add key value pairs to a dictionary/list called itemData.

My issue is that I get KeyNotFoundException: The given key was not present in the dictionary if, for example, the key value pair ["listings"]["names"]["firstName"] doesn't exist in the Json object (sometime it wont, it is a dynamic json file.)

is there a simple method to say, if this key exists - proceed with function?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions