LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.36k stars 403 forks source link

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 4 years ago

corbinyo commented 4 years ago

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?