Deserialize JSON with Newtonsoft for different answers

0

I have the following problem.
I make a request rest to a service, e.g. link the answer is like this if it exists:

{ 
  "Id": 7113,
  "Name": "Ted Norris",
  "BirthDate": "1977-05-13T00:00:00",
  "Phone": "488-555-1212",
  "Address": {
  "Street": "123 Main St",
  "City": "San Diego",
  "State": "CA",
  "Zip": "92115"
}

and if there is any error this other answer

{ 
  "IsSuccess": false,
  "Message": "Can not connect to database"
}

How can I deserialize two different objects, what would be the best way to do it.
 If it can be Using the library Newtonsoft.Json

thank you very much.

    
asked by ja73 16.03.2017 в 21:55
source

1 answer

4

You could put any of the two JSON objects in a variable of type JObject . As follows:

string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject obj = JObject.Parse(json);

Source: link

Then to access the values simply:

var prop = obj["nombre_propiedad"];
if(prop != null){
    // hacer algo aqui
}
    
answered by 16.03.2017 / 22:04
source