at010
at010

Reputation: 137

Extract data from a JSONObject response

I have the following response and I am trying to extract the id under sale and related_resources.

So far I have tried to retrieve this id with the below, but I think I am going off the wrong tracks by trying to get an array twice (reason being they had a [ infront of them.

confirm.toJSONObject().getJSONObject("response").getJSONArray("transactions").getJSONArray("related_resources")

Eclipse then shows the following message The method getJSONArray(int) in the type JSONArray is not applicable for the arguments (String) What is the correct approach to extract the required data?

{
  "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"
    }
  ]
}

UPDATE: I can now extract the sale id, but it feels like a hack. Is there a better approach:

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

Upvotes: 1

Views: 1358

Answers (1)

Darren
Darren

Reputation: 10398

getJSONArray("transactions") returns an array. You can only get an object from the array by its location. So you need to get the object at index 0 first (the first object in the array), then on that you can call getJSONArray("related_resources")

I'm not really a Java programmer but I'm guessing .get(0) would do the trick.

An easy way to find where you are going wrong, is split each part into its own var and print them.

Array transactions = getJSONArray("transactions");
Log.v(0, transactions);
Array related = transactions.get(0).getJSONArray("related_resources");
Log.v(0, related);

Upvotes: 2

Related Questions