Get value from JToken that may not exist (best practices)

前端 未结 6 1626
说谎
说谎 2020-12-12 13:11

What\'s the best practice for retrieving JSON values that may not even exist in C# using Json.NET?

Right now I\'m dealing with a JSON provider that returns JSON that

6条回答
  •  天涯浪人
    2020-12-12 13:52

    Here is how you can check if the token exists:

    if (jobject["Result"].SelectToken("Items") != null) { ... }
    

    It checks if "Items" exists in "Result".

    This is a NOT working example that causes exception:

    if (jobject["Result"]["Items"] != null) { ... }
    

提交回复
热议问题