我是调用 API 的新手。我在 Java 中使用 Retrofit 调用 API。有时我得到那种类型的对象。
{
"error": 0,
"msg": "Success",
"data": {
"request_id": 0000,
"request_status": "Complete",
"request_charge": "0.0000",
"recipients": [
{
"number": "8801800000000",
"charge": "0.0000",
"status": "Sent"
}
]
}
}
在这个 JSON 中,我如何使用数据对象“mag”以及收件人对象。
我需要 mag、request_status、number 和 status 的字符串值
我该怎么做?
在普通 Javascript 中,如果您将上述 JSON 对象作为 JSON 字符串保存在名为 myJson 的变量中,只需使用 JSON.parse 将其转换为 JS 对象:
let myObject = JSON.parse(myJson);
如果JSON对象不是字符串,已经是JS对象,以上略过
然后简单地访问对象的属性:
console.log(myObject.msg); // "Success"
console.log(myObject.data.recipients[2]); // "Sent"