如何使用包含更多 JSON 对象的 JSON 对象。

huangapple go评论65阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年5月17日 15:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定