如何从 API JSON-RPC 响应的结果中获取变量的值
How to grab values to a variable from the result of API JSON-RPC response
{
"jsonrpc": "2.0",
"id": "123",
"result": {
"columns": [
"Unit Number",
"Tank Number",
"Volume",
"Volume Percent",
"Description",
"Capacity",
"Status",
"Last Updated"
],
"values": [
[
null,
null,
"0",
"0",
"Tank 1",
"50000",
"N/A",
"1970-01-01 09:30:00"
],
[
"3376",
"1",
"18490",
"68.4815",
"SmartFill 3376 Tank 1",
"27000",
"Offline",
"2018-06-06 14:28:20"
]
]
}
}
我相信您正在寻找 JSON.parse()
。
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
const value = JSON.parse(`{ "jsonrpc": "2.0", "id": "123", "result": { "columns": [ "Unit Number", "Tank Number", "Volume", "Volume Percent", "Description", "Capacity", "Status", "Last Updated" ], "values": [ [ null, null, "0", "0", "Tank 1", "50000", "N/A", "1970-01-01 09:30:00" ], [ "3376", "1", "18490", "68.4815", "SmartFill 3376 Tank 1", "27000", "Offline", "2018-06-06 14:28:20" ] ] } }`);
console.log(value.result.values);
希望这对您有所帮助,
{
"jsonrpc": "2.0",
"id": "123",
"result": {
"columns": [
"Unit Number",
"Tank Number",
"Volume",
"Volume Percent",
"Description",
"Capacity",
"Status",
"Last Updated"
],
"values": [
[
null,
null,
"0",
"0",
"Tank 1",
"50000",
"N/A",
"1970-01-01 09:30:00"
],
[
"3376",
"1",
"18490",
"68.4815",
"SmartFill 3376 Tank 1",
"27000",
"Offline",
"2018-06-06 14:28:20"
]
]
}
}
我相信您正在寻找 JSON.parse()
。
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
const value = JSON.parse(`{ "jsonrpc": "2.0", "id": "123", "result": { "columns": [ "Unit Number", "Tank Number", "Volume", "Volume Percent", "Description", "Capacity", "Status", "Last Updated" ], "values": [ [ null, null, "0", "0", "Tank 1", "50000", "N/A", "1970-01-01 09:30:00" ], [ "3376", "1", "18490", "68.4815", "SmartFill 3376 Tank 1", "27000", "Offline", "2018-06-06 14:28:20" ] ] } }`);
console.log(value.result.values);
希望这对您有所帮助,