Possible to look for Key that does not exist in Json.net

前端 未结 3 627
故里飘歌
故里飘歌 2020-12-16 11:10

I got a couple different formats that come in but I can\'t figure out how to handle them all because when I try to find by key json.net crashes. I was hoping it would just r

3条回答
  •  温柔的废话
    2020-12-16 12:02

    Or you can simply use the ContainsKey on a JsonObject. Here is a sample of my own code with similar problem to yours:

    foreach (JsonObject feed in data)
                {
                    var fbFeed = new FacebookFeeds();
                    if (feed.ContainsKey("message"))
                        fbFeed.Message = (string)feed["message"];
                    if (feed.ContainsKey("story"))
                        fbFeed.Message = (string)feed["story"];
                    if (feed.ContainsKey("picture"))
                        fbFeed.Message = (string)feed["picture"];
                    fbFeeds.Add(fbFeed);
                }
    

提交回复
热议问题