从JSONObject响应中提取数据

时间:2015-01-03 23:15:49

标签: java json

我有以下回复,我正在尝试提取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"
    }
  ]
}

更新: 我现在可以提取销售ID,但感觉就像是黑客。有没有更好的方法:

            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);

1 个答案:

答案 0 :(得分:2)

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);