Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
corbinyo opened this issue Sep 3, 2020 · 0 comments

Comments

@corbinyo
Copy link

corbinyo commented Sep 3, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant