Ethereum JSON RPC Java
Publish: 2021-04-29 06:28:28
1. Ethereum also uses blockchain technology, but it is more convenient to use than bitcoin's blockchain Turing, which is complete and open source. Anyone can create their own application on the basis of Ethereum
2. Send according to the standard JSON string format
JSON RPC is a standard
wiki.geekdream.com/specification/json RPC_ 2.0. HTML
here is the official document of 2.0. In addition, the package for parsing needs to be introced
JSON RPC is a standard
wiki.geekdream.com/specification/json RPC_ 2.0. HTML
here is the official document of 2.0. In addition, the package for parsing needs to be introced
3. Unknown_Error
4. JSONObject jsonObject = new JSONObject();
Map map = request.getParameterMap();
Iterator<?> it = map.keySet().iterator();
while(it.hasNext()){
String key = (String)it.next();
String[] values = (String[])map.get(key);
jsonObject.accumulate(key, values[0]);
}
String name = jsonObject.getString("userName"); //返回从前台接受的用户名
System.out.println(name); //输出用户名
jsonObject.clear(); // 清空jsonObjec中的数据
jsonObject.put("love" , "足球"); //将足球赋给love这个变量名
out.print(jsonObject); //返回json格式的数据
Map map = request.getParameterMap();
Iterator<?> it = map.keySet().iterator();
while(it.hasNext()){
String key = (String)it.next();
String[] values = (String[])map.get(key);
jsonObject.accumulate(key, values[0]);
}
String name = jsonObject.getString("userName"); //返回从前台接受的用户名
System.out.println(name); //输出用户名
jsonObject.clear(); // 清空jsonObjec中的数据
jsonObject.put("love" , "足球"); //将足球赋给love这个变量名
out.print(jsonObject); //返回json格式的数据
5. Json-rpc-java is composed of two components for user visualization. They are jsonrpcbridge and jsonrpcservlet. The coordination of the two components realizes the processing of request objects by JSON server and responds to users
jsonrpcbridge is a session object that has the reference of the object exported from the server to a specific client. It receives the json-rpc request from the server (that is, jsonrpcservelet), and then decodes the JSON object into Java object, and then plays the role of method call, In addition, the Java object result group code (Marshall) returned by the method call is passed to the client for processing as a JSON object. The specific type conversion from Java object to JavaScript object is handled by serializer, which is responsible for serialization
jsonrpcbridge must be placed in the httpsession object, and the registered property name is "jsonrpcbridge", so that jsonrpcservlet can locate the bridge responsible for calling Java objects exported to the client. Therefore, in order to export all instances and static methods of an object to the client, you should have the following code: jsonrpcbridge. Registerobject (& quot; myObject", myObject)
in order to export all static methods of a class, you should: jsonrpcbridge. Registerclass (& quot; myObject", myObject)
if registerobject and registerclass are called multiple times to be used by objects with the same key value, they will be updated by the latest assigned object
in JSON, you can also use global bridge in singleton mode to export all instance methods for all HTTP clients. It can be used to register factory class, but we should pay attention to authentication and security when using it. When used, the identifier is as follows: jsonrpcbridge. Getglobalbridge(). Registerobject (& quot; myObject", myObject); As above, it can also export all static methods
the most important part of this protocol is servlet. Now let's briefly introce it:
in this protocol, jsonrpcservelet acts as a transmitter to process the JSON transmitted over HTTP
jsonrpcbridge is a session object that has the reference of the object exported from the server to a specific client. It receives the json-rpc request from the server (that is, jsonrpcservelet), and then decodes the JSON object into Java object, and then plays the role of method call, In addition, the Java object result group code (Marshall) returned by the method call is passed to the client for processing as a JSON object. The specific type conversion from Java object to JavaScript object is handled by serializer, which is responsible for serialization
jsonrpcbridge must be placed in the httpsession object, and the registered property name is "jsonrpcbridge", so that jsonrpcservlet can locate the bridge responsible for calling Java objects exported to the client. Therefore, in order to export all instances and static methods of an object to the client, you should have the following code: jsonrpcbridge. Registerobject (& quot; myObject", myObject)
in order to export all static methods of a class, you should: jsonrpcbridge. Registerclass (& quot; myObject", myObject)
if registerobject and registerclass are called multiple times to be used by objects with the same key value, they will be updated by the latest assigned object
in JSON, you can also use global bridge in singleton mode to export all instance methods for all HTTP clients. It can be used to register factory class, but we should pay attention to authentication and security when using it. When used, the identifier is as follows: jsonrpcbridge. Getglobalbridge(). Registerobject (& quot; myObject", myObject); As above, it can also export all static methods
the most important part of this protocol is servlet. Now let's briefly introce it:
in this protocol, jsonrpcservelet acts as a transmitter to process the JSON transmitted over HTTP
6. 服务端示例:
<?php
namespace Rpc\Controller;
use Think\Controller\JsonRpcController;
class JsonApiController extends JsonRpcController
{
public function index(){
return 'Hello, JsonRPC!';
}
// 支持参数传入
public function test($name=''){
return "Hello, {$name}!";
}
}
客户端示例:
vendor('jsonRPC.jsonRPCClient');
$client = new \jsonRPCClient('http://www.tp.cn/index.php/Rpc/JsonApi');
$result = $client->index();
var_mp($result); // 结果:Hello, JsonRPC!
$result = $client->test('deeka');
var_mp($result); // 结果:Hello, deeka!
<?php
namespace Rpc\Controller;
use Think\Controller\JsonRpcController;
class JsonApiController extends JsonRpcController
{
public function index(){
return 'Hello, JsonRPC!';
}
// 支持参数传入
public function test($name=''){
return "Hello, {$name}!";
}
}
客户端示例:
vendor('jsonRPC.jsonRPCClient');
$client = new \jsonRPCClient('http://www.tp.cn/index.php/Rpc/JsonApi');
$result = $client->index();
var_mp($result); // 结果:Hello, JsonRPC!
$result = $client->test('deeka');
var_mp($result); // 结果:Hello, deeka!
7. There are two kinds of structure in JSON Construction:
in brief, JSON is the object and array in JavaScript, so these two kinds of structure are object and array. Through these two kinds of structure, various complex structures can be represented
1. Object: object is represented as "{}" in JS, and data structure is the structure of {key: value, key: value,...} key value pairs, In the object-oriented language, key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is the object. Key gets the attribute value, and the type of the attribute value can be number, string, array and object. In Java, it is a map structure
2. Array: in JS, the array is expanded by bracket "[]", and the data structure is [& quot; java",& quot; javascript",& quot; vb",...], The method of value taking is the same as that in all languages. Index is used to get the field value. The type of field value can be number, string, array and object. In Java, it is a list structure
complex data structures can be formed by the combination of object and array structures< Second, create objects with JSON in JavaScript
var people = {& quot; name":& quot; jack",& quot; sex":& quot; mail"};
you can use this syntax to create JSON objects, which are enclosed by "{}" and separated by "," key: value
use JSON syntax to create arrays
var arr = [value1, Value2...]
an array of people = [{& quot; name":& quot; jack",& quot; sex":& quot; mail"},{& quot; name":& quot; lily",& quot; sex":& quot; femail"};
JSON arrays can be created with this syntax, using & quot; []& quot; The syntax is as follows:
when you use the JSON format string to interact with the server, you can use the JSON extension method to convert the string into a JavaScript object or an object into a JSON format string. But first log in http://www.json.org Download the json2. JS file. After the package is introced, the JS object is directly changed into a string format with tojsonstring(), and the string can be converted into a JS object with parsejson()
access data
you only need to use dot notation to represent array elements. Therefore, to access the name of the first entry in the people list above, you only need to use the following code in javascript:
people [0]. Name
with this syntax, you can process any data in JSON format without using any additional JavaScript toolkit or API
modify data
just as you can access data with dot and square brackets, you can easily modify data in the same way:
people [0]. Name = & quot; Rachmaninov";
after converting a string to a JavaScript object, you can modify the data in the variable like this.
in brief, JSON is the object and array in JavaScript, so these two kinds of structure are object and array. Through these two kinds of structure, various complex structures can be represented
1. Object: object is represented as "{}" in JS, and data structure is the structure of {key: value, key: value,...} key value pairs, In the object-oriented language, key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is the object. Key gets the attribute value, and the type of the attribute value can be number, string, array and object. In Java, it is a map structure
2. Array: in JS, the array is expanded by bracket "[]", and the data structure is [& quot; java",& quot; javascript",& quot; vb",...], The method of value taking is the same as that in all languages. Index is used to get the field value. The type of field value can be number, string, array and object. In Java, it is a list structure
complex data structures can be formed by the combination of object and array structures< Second, create objects with JSON in JavaScript
var people = {& quot; name":& quot; jack",& quot; sex":& quot; mail"};
you can use this syntax to create JSON objects, which are enclosed by "{}" and separated by "," key: value
use JSON syntax to create arrays
var arr = [value1, Value2...]
an array of people = [{& quot; name":& quot; jack",& quot; sex":& quot; mail"},{& quot; name":& quot; lily",& quot; sex":& quot; femail"};
JSON arrays can be created with this syntax, using & quot; []& quot; The syntax is as follows:
when you use the JSON format string to interact with the server, you can use the JSON extension method to convert the string into a JavaScript object or an object into a JSON format string. But first log in http://www.json.org Download the json2. JS file. After the package is introced, the JS object is directly changed into a string format with tojsonstring(), and the string can be converted into a JS object with parsejson()
access data
you only need to use dot notation to represent array elements. Therefore, to access the name of the first entry in the people list above, you only need to use the following code in javascript:
people [0]. Name
with this syntax, you can process any data in JSON format without using any additional JavaScript toolkit or API
modify data
just as you can access data with dot and square brackets, you can easily modify data in the same way:
people [0]. Name = & quot; Rachmaninov";
after converting a string to a JavaScript object, you can modify the data in the variable like this.
8. Look at the dopost in it
Hot content
