从 JSONObject 响应中提取数据
Extract data from a JSONObject response
我收到以下回复,我正在尝试提取 sale
和 related_resources
下的 id
。
到目前为止,我已经尝试用下面的方法检索这个 id,但我认为我试图两次获取一个数组而走错了路(原因是他们前面有一个 [
。
confirm.toJSONObject().getJSONObject("response").getJSONArray("transactions").getJSONArray("related_resources")
Eclipse 然后显示以下消息The method getJSONArray(int) in the type JSONArray is not applicable for the arguments (String)
提取所需数据的正确方法是什么?
{
"id": "PAY-17S8410768582940NKEE66EQ",
"create_time": "2013-01-31T04:12:02Z",
"update_time": "2013-01-31T04:12:04Z",
"state": "approved",
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [
{
"credit_card": {
"type": "visa",
"number": "xxxxxxxxxxxx0331",
"expire_month": "11",
"expire_year": "2018",
"first_name": "Betsy",
"last_name": "Buyer",
"billing_address": {
"line1": "111 First Street",
"city": "Saratoga",
"state": "CA",
"postal_code": "95070",
"country_code": "US"
}
}
}
]
},
"transactions": [
{
"amount": {
"total": "7.47",
"currency": "USD",
"details": {
"tax": "0.03",
"shipping": "0.03"
}
},
"description": "This is the payment transaction description.",
"related_resources": [
{
"sale": {
"id": "4RR959492F879224U",
"create_time": "2013-01-31T04:12:02Z",
"update_time": "2013-01-31T04:12:04Z",
"state": "completed",
"amount": {
"total": "7.47",
"currency": "USD"
},
"parent_payment": "PAY-17S8410768582940NKEE66EQ",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
"rel": "self",
"method": "GET"
}
]
}
更新:
我现在可以提取销售 ID,但感觉就像一个 hack。有没有更好的办法:
JSONObject obj = new JSONObject(input.toString());
String temp = obj.getJSONArray("transactions").getJSONObject(0)
.getJSONArray("related_resources").getJSONObject(0)
.toString();
JSONObject obj2 = new JSONObject(temp);
String temp2 = obj2.getJSONObject("sale").getString("id");
System.out.println(temp);
System.out.println(temp2);
getJSONArray("transactions")
returns 一个数组。您只能通过其位置从数组中获取对象。所以你需要先获取索引为 0 的对象(数组中的第一个对象),然后你可以调用 getJSONArray("related_resources")
我不是真正的 Java 程序员,但我猜 .get(0)
会成功。
找到错误位置的一种简单方法是将每个部分拆分为自己的 var 并打印它们。
Array transactions = getJSONArray("transactions");
Log.v(0, transactions);
Array related = transactions.get(0).getJSONArray("related_resources");
Log.v(0, related);
我收到以下回复,我正在尝试提取 sale
和 related_resources
下的 id
。
到目前为止,我已经尝试用下面的方法检索这个 id,但我认为我试图两次获取一个数组而走错了路(原因是他们前面有一个 [
。
confirm.toJSONObject().getJSONObject("response").getJSONArray("transactions").getJSONArray("related_resources")
Eclipse 然后显示以下消息The method getJSONArray(int) in the type JSONArray is not applicable for the arguments (String)
提取所需数据的正确方法是什么?
{
"id": "PAY-17S8410768582940NKEE66EQ",
"create_time": "2013-01-31T04:12:02Z",
"update_time": "2013-01-31T04:12:04Z",
"state": "approved",
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [
{
"credit_card": {
"type": "visa",
"number": "xxxxxxxxxxxx0331",
"expire_month": "11",
"expire_year": "2018",
"first_name": "Betsy",
"last_name": "Buyer",
"billing_address": {
"line1": "111 First Street",
"city": "Saratoga",
"state": "CA",
"postal_code": "95070",
"country_code": "US"
}
}
}
]
},
"transactions": [
{
"amount": {
"total": "7.47",
"currency": "USD",
"details": {
"tax": "0.03",
"shipping": "0.03"
}
},
"description": "This is the payment transaction description.",
"related_resources": [
{
"sale": {
"id": "4RR959492F879224U",
"create_time": "2013-01-31T04:12:02Z",
"update_time": "2013-01-31T04:12:04Z",
"state": "completed",
"amount": {
"total": "7.47",
"currency": "USD"
},
"parent_payment": "PAY-17S8410768582940NKEE66EQ",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/4RR959492F879224U/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-17S8410768582940NKEE66EQ",
"rel": "self",
"method": "GET"
}
]
}
更新: 我现在可以提取销售 ID,但感觉就像一个 hack。有没有更好的办法:
JSONObject obj = new JSONObject(input.toString());
String temp = obj.getJSONArray("transactions").getJSONObject(0)
.getJSONArray("related_resources").getJSONObject(0)
.toString();
JSONObject obj2 = new JSONObject(temp);
String temp2 = obj2.getJSONObject("sale").getString("id");
System.out.println(temp);
System.out.println(temp2);
getJSONArray("transactions")
returns 一个数组。您只能通过其位置从数组中获取对象。所以你需要先获取索引为 0 的对象(数组中的第一个对象),然后你可以调用 getJSONArray("related_resources")
我不是真正的 Java 程序员,但我猜 .get(0)
会成功。
找到错误位置的一种简单方法是将每个部分拆分为自己的 var 并打印它们。
Array transactions = getJSONArray("transactions");
Log.v(0, transactions);
Array related = transactions.get(0).getJSONArray("related_resources");
Log.v(0, related);