简体   繁体   中英

Function to build JSON-RPC

Is there a function to build JSON-RPC in Javascript without libraries but not actually send it? I want to send it over Websocket.

Just create a JSON object, then use JSON.stringify to turn it into a string if that's what you're looking for.

For example, to build the wikipedia v1.1 example:

{
    "version": "1.1",
    "method": "confirmFruitPurchase",
    "id": "194521489",
    "params": [
        ["apple", "orange", "Mangoos"],
        1.123
    ]
}

You could do this:

var rpc = {
    version: 1.1,
    method: "confirmFruitPurchase",
    id: 194521489,
}

etc.

You can modify this like you would a normal javascript object using . or [] notation then var JSON_RPC_string = JSON.stringify(rpc) when you are ready to send it.

I don't think you need a library to create these, but you could create your own simple helper functions if there is something you keep repeating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM