[Delphi]View PlainCopy
- Procedure TFORM5. Button4click (Sender:tobject);
- Var
- O, Item:isuperobject;
- Strm:tstringstream;
- Result: string;
- Ctx:tsuperrtticontext;
- Student:tstudent;
- Begin
- //can refer to Superobject's readme.html
- //json Demo Https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29
- //{"JSONRPC": "2.0", "method": "Fuck", "params": ["hehe", 32,4], "id": "1433813750019"}
- //O: = So (' {"JSONRPC": "2.0", "method": "Fuck", "params": ["hehe", 32,4], "id": "1433813750019"} ');
- //O: = So (' {"JSONRPC": "2.0", "method": Fuck, "params": ["hehe", 32,4]} ');
- O: = SO (
- ' {"JSONRPC": "2.0", "method": "Fuck", "params": ["hehe", 32,4], "id": "12"} ');
- STRM: = Tstringstream. Create (O. asstring);
- Try
- IdHTTP1. Request. ContentType: = ' Application/json ';
- Result: = IdHTTP1. Post (' http://10.0.0.107/json_server/server.php ', STRM);
- Memo1. Lines. ADD (result);
- O: = SO (result);
- Memo1. Lines. ADD (O. Asobject. s[' result ');
- O: = SO (
- ' {"JSONRPC": "2.0", "Method": "Getstudent", "params": [{"id": 1, "name": "Name", "Age": 123}], "id": "1433813750240"} ' );
- STRM: = Tstringstream. Create (O. asstring);
- Result: = IdHTTP1. Post (' http://10.0.0.107/json_server/server.php ', STRM);
- Memo1. Lines. ADD (result);
- O: = SO (result);
- Result: = O. Asobject. s[' result ';
- Memo1. Lines. ADD (result);
- CTX: = Tsuperrtticontext. Create;
- Try
- //JSON conversion to object
- Student: = CTX. Astype<tstudent> (SO (result));
- ShowMessage (student. Name);
- //Object converted to JSON
- O: = CTX. Asjson<tstudent> (student);
- ShowMessage (O. asstring);
- finally
- //CTX. Free;
- end;
- O: = SO (
- ' {' Jsonrpc ': ' 2.0 ', ' method ': ' getstudents ', ' params ': [' xxx '], ' id ': ' 1433814568751 '} ';
- STRM: = Tstringstream. Create (O. asstring);
- Result: = IdHTTP1. Post (' http://10.0.0.107/json_server/server.php ', STRM);
- Memo1. Lines. ADD (result);
- O: = SO (result);
- Memo1. Lines. ADD (O. Asobject. s[' result ');
- For item in o[' result ') do
- begin
- Student: = CTX. Astype<tstudent> (item);
- ShowMessage (student. Name);
- //ShowMessage (item. asstring);
- end;
- finally
- STRM. Free;
- end;
- End
http://blog.csdn.net/earbao/article/details/46423167
Delphi uses Superobject to implement HTTP remote invocation of JSONRPC good