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

前端 未结 6 1633
说谎
说谎 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:59

    This is pretty much what the generic method Value() is for. You get exactly the behavior you want if you combine it with nullable value types and the ?? operator:

    width = jToken.Value("width") ?? 100;
    

提交回复
热议问题