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

前端 未结 6 1639
说谎
说谎 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 14:05

    You can simply typecast, and it will do the conversion for you, e.g.

    var with = (double?) jToken[key] ?? 100;
    

    It will automatically return null if said key is not present in the object, so there's no need to test for it.

提交回复
热议问题