我有以下响应,我正在尝试提取idsale下的related_resources

到目前为止,我尝试使用下面的方法检索此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"
    }
  ]
}


更新:
我现在可以提取销售编号,但感觉像是被黑客入侵了。有没有更好的方法:

            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")返回一个数组。您只能通过数组的位置获取对象。因此,您需要首先获取索引为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);

10-02 20:42