The JSON deserialization class in restsharp is jsondeserializer. Its usage is a little different from that in JSON. net.
(1) set rootelement before deserialization; otherwise, deserialization fails.
(2) If there are several group member variables in the object attributes after deserialization, you must use list <string> instead of string []. Otherwise, the system will prompt that no constructor without parameters is defined.
The following example is used to deserialize a json string.
JSON string: {"error": {"code": 400, "message": "unable to complete operation.", "details": ["invalid URL"]}
The. NET code is as follows:
Public class jsondemo
{
Public void test ()
{
VaR myresponse = new restresponse ();
Myresponse. content = "{\" error \ ": {\" Code \ ": 400, \" message \ ": \" unable to complete operation. \ ", \" details \ ": [\" invalid URL \ "]}";
Jsondeserializer myjsondeserializer = new jsondeserializer ();
Myjsondeserializer. rootelement = "error ";
List <errorobject> myerrors = myjsondeserializer. deserialize <list <errorobject> (myresponse );
}
}
Public class errorobject
{
Private int _ code;
Public int code
{
Get {return _ code ;}
Set {_ code = value ;}
}
Private string _ message;
Public String message
{
Get {return _ message ;}
Set {_ message = value ;}
}
Private list <string> _ details;
Public list <string> details
{
Get {return _ details ;}
Set {_ details = value ;}
}
Public errorobject ()
{}
}