如何访问此 JSON 对象中的嵌套值?
How do I access a nested value inside this JSON object?
{
"payload": "{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-07-12T18:04:45.901Z\",\"returnResult\":\"success\",\"resultDescription\":\"\",\"blockHash\":\"00000000000000000096b13c9aee9b1a0b02daecf8200a10616adf1349eefd4f\",\"blockHeight\":641659,\"confirmations\":1723,\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"txSecondMempoolExpiry\":0}",
"signature": "3045022100cf2a2e272ae02e95a37dc010342d759714a22c1bda9761807049f46ce3962f14022074e70938e06ac8095498007f1e4a84373226c2928b7d26d97b337d6328b59c0c",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270",
"encoding": "UTF-8",
"mimetype": "application/json" }
我目前正在使用 Javascript 进行一个项目。在我的逻辑中,我进行了一次提取,将事务数据发布到 MAPI,即 returns 上面列出的 JSON 对象。我正在尝试访问 'payload' 键中的特定值。为了这个问题 - 我们可以访问时间戳值吗?
然后我想将这个值保存到一个变量中,以便我可以将它用于其他任务。
我的代码:
async function postData(url ='', data = { }) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json' },
body: JSON.stringify(data) });
let txoData = await response.json();
let timestamp = Object.values(Object.values(txoData)[0]['timestamp'])
return txoData;
}
postData('https://merchantapi.taal.com/mapi/tx', { 'rawtx': raw1 })
.then(txoData => { console.log(txoData) })
.then(timestamp => { console.log(timestamp) })`
我之前的代码返回“undefined”
获得 json 对象 (response.json()) 后,您可以通过以下方式了解属性名称来访问属性:
var a = jsonObj.propertyName;
或者这样:
var a = jsonObj['propertyName'];
response.json() returns json 对象,这意味着字符串数据可作为对象使用,因此对象表示法可用于使用适当的嵌套 属性 名字。
所以,下面应该可行
let txoData = await response.json();
let timestamp = txoData.payload.timestamp;
{
"payload": "{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-07-12T18:04:45.901Z\",\"returnResult\":\"success\",\"resultDescription\":\"\",\"blockHash\":\"00000000000000000096b13c9aee9b1a0b02daecf8200a10616adf1349eefd4f\",\"blockHeight\":641659,\"confirmations\":1723,\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"txSecondMempoolExpiry\":0}",
"signature": "3045022100cf2a2e272ae02e95a37dc010342d759714a22c1bda9761807049f46ce3962f14022074e70938e06ac8095498007f1e4a84373226c2928b7d26d97b337d6328b59c0c",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270",
"encoding": "UTF-8",
"mimetype": "application/json" }
我目前正在使用 Javascript 进行一个项目。在我的逻辑中,我进行了一次提取,将事务数据发布到 MAPI,即 returns 上面列出的 JSON 对象。我正在尝试访问 'payload' 键中的特定值。为了这个问题 - 我们可以访问时间戳值吗?
然后我想将这个值保存到一个变量中,以便我可以将它用于其他任务。
我的代码:
async function postData(url ='', data = { }) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json' },
body: JSON.stringify(data) });
let txoData = await response.json();
let timestamp = Object.values(Object.values(txoData)[0]['timestamp'])
return txoData;
}
postData('https://merchantapi.taal.com/mapi/tx', { 'rawtx': raw1 })
.then(txoData => { console.log(txoData) })
.then(timestamp => { console.log(timestamp) })`
我之前的代码返回“undefined”
获得 json 对象 (response.json()) 后,您可以通过以下方式了解属性名称来访问属性:
var a = jsonObj.propertyName;
或者这样:
var a = jsonObj['propertyName'];
response.json() returns json 对象,这意味着字符串数据可作为对象使用,因此对象表示法可用于使用适当的嵌套 属性 名字。
所以,下面应该可行
let txoData = await response.json();
let timestamp = txoData.payload.timestamp;