c# - JSON.NET - throw exception if JSON-data have no relation to desirable type -


I am using the JSON web-service via any web-service error type JSON or Response type via JSON request Can answer from For example: "response": "registration", "error": {"@ c": "error", "code": "USER_EXISTS", "message": ""}}

  {"response": "register", "register": {"session": {"@c": "session", "UserId": "18", "SessionId": "ASFT23WETT234QQW"}}   

Any feedback that I am trying to deserialize to type, which is related to the request, for example, when the registration request registers Register is there.

If the response is an error type response, the code

  JsonConvert.DeserializeObject & lt; RegisterResponse & gt; (JsonResponse);   

If there is some inconsistency with the JSON string and the target type, how is JSON.NET suggested for throwing exceptions?

class definitions:

  public class feedback {get public string feedback { Set; } Public Response () {}} Public Season Session {Public long userId; Public string session id; Public Session () {}} Public Class Registration Response: Response {Public Session Session {Received; Set; } Public Register Response () {Session = New Session (); }} Public class error {public string code; Public string message; Public error () {}}  

This is not directly a JSON.NET bag because the conversion What you gave was completely legitimate, you have given it poor data to do the best job, just to check your data first. Either create a error category and replace it with

  Jason Convert. Directoryal Object & lt; Error & gt; (JsonResponse); To ensure the properties of   

and then error , test that they hope in an empty way.

Alternatively, you just decode an error property that you see is whether it comes back or not.

  Jobbject o = Jobbject.Parse (jsonResponse); String error = (string) o ["error"]; If (string.IsNullOrEmpty (error) == incorrect) // Bad things are going down / if you come here then it's all good    

Comments