英文:
How to use json object that contains more json object
问题
In this JSON, you can access the following values:
- "msg": Use this to get the value of "msg" which is "Success".
- "request_status": Use this to get the value of "request_status" which is "Complete".
- "number": Use this to get the value of "number" which is "8801800000000".
- "status": Use this to get the value of "status" which is "Sent".
To access these values in Java using Retrofit, you would typically create corresponding Java classes that mirror the structure of the JSON, and then use these classes to parse the JSON response. If you need specific Java code for parsing this JSON using Retrofit, please provide the Java code context you have so far, and I can assist further.
英文:
I am new to the calling API. I am calling API using Retrofit in Java. Sometimes I get that type of object.
{
"error": 0,
"msg": "Success",
"data": {
"request_id": 0000,
"request_status": "Complete",
"request_charge": "0.0000",
"recipients": [
{
"number": "8801800000000",
"charge": "0.0000",
"status": "Sent"
}
]
}
}
In this JSON how can I use "mag", the data object, and also use the recipients object.
I need the string value of mag, request_status, number, and status.
How can I do this?
答案1
得分: -1
让我来为你翻译代码部分:
// 将上述JSON对象作为JSON字符串存储在名为myJson的变量中,在普通的Javascript中,只需使用JSON.parse将其转换为JS对象:
let myObject = JSON.parse(myJson);
// 如果JSON对象不是一个字符串,而是已经是一个JS对象,则跳过上述步骤。
// 然后简单地访问对象的属性:
console.log(myObject.msg); // "Success"
console.log(myObject.data.recipients[2]); // "Sent"
英文:
In plain Javascript, if you have the above JSON object as a JSON string in a variable called myJson, just use JSON.parse to convert it into a JS object:
let myObject = JSON.parse(myJson);
If the JSON object is not a string, but already a JS object, skip the above.
Then simply access the properties of the object:
console.log(myObject.msg); // "Success"
console.log(myObject.data.recipients[2]); // "Sent"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论