Deserialize json in a “TryParse” way

后端 未结 6 640
走了就别回头了
走了就别回头了 2020-11-28 09:14

When I send a request to a service (that I do not own), it may respond either with the JSON data requested, or with an error that looks like this:

{
    \"er         


        
6条回答
  •  鱼传尺愫
    2020-11-28 09:28

    You may deserialize JSON to a dynamic, and check whether the root element is error. Note that you probably don't have to check for the presence of status and code, like you actually do, unless the server also sends valid non-error responses inside a error node.

    Aside that, I don't think you can do better than a try/catch.

    What actually stinks is that the server sends an HTTP 200 to indicate an error. try/catch appears simply as checking of inputs.

提交回复
热议问题