how to read json within json in ios - ios

i have the being fetched, i need to read whats inside data under sources
"id": "cus_7nDkZw63KvTuy5",
"object": "customer",
"account_balance": 0,
"created": 1453839669,
"currency": "usd",
"default_source": "card_17XdJE2eZvKYlo2CBNnle4YM",
"delinquent": false,
"description": null,
"discount": null,
"email": "someone#example.com",
"livemode": false,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [
{
"id": "card_17XdJE2eZvKYlo2CBNnle4YM",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_7nDkZw63KvTuy5",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2016,
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "someone#example.com",
"tokenization_method": null
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/customers/cus_7nDkZw63KvTuy5/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_7nDkZw63KvTuy5/subscriptions"
}
}

Try this code you will get array
[[[[result objectForKey:#"sources"] objectForKey:#"data"] objectAtIndex:0] objectForKey:#"country"];
you will get US as answer.

Related

For Loop for checking whether specific id is found in a array

I need to check the id in a array list , id has been generated previously and just I need to compare with the response generated later, For example: take "defaultcardid" is the previous generated id, so I need to compare the "defaultcard" with later response.
I am getting a "cardlist" as a "Anyobject" response. so need to check "defaultcardid" inside "cardlist" array.
if let xlist = (cardlist.value(forKey: "data")as? NSArray)
{
for 0 .. i in xlist
{
cardfinder.adding((xlist.object(at: id)as AnyObject).value(forKey: "id")as! String)
}
}
cardfinder is a NSMUTABLEARRAY to store the particular id into an array.
My son response is below
{
"status": "success",
"data": [
{
"id": "card_1D42hlLJSnYRR6BMULHyeobB",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "FwLfSpHMDz1b3PTH",
"funding": "debit",
"last4": "5556",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D441WLJSnYRR6BMNpOsFgg0",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "dvLGAxROCikDisQx",
"funding": "credit",
"last4": "0093",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D42RjLJSnYRR6BMgSELUUut",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "NnWxCq6FamUKl226",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
}
]
}
All the objects have field called "id" I already have a default id = card_1D42RjLJSnYRR6BMgSELUUut , I need to exactly take in which object my card is .
so I need a for loop for checking all the objects in array.
You can simply call contains method on array to check whether a value exist in it. Implementation depends upon the type of array you are checking.
Example:
Let's assume "123" is the default id.
var cardList1 = ["123", "abc", "xyz"]
cardList1.contains("123") //true
cardList1.contains("456") //false
var cardList2 = [["id": "123"], ["id": "abc"], ["id": "xyz"]]
cardList2.contains { //true
return $0["id"] == "123"
}
cardList2.contains { //false
return $0["id"] == "564"
}
Let me know in case you face any issue with this.

json data stripe webhook

Im trying to implement stripe webhooks in my rails app. The webhook send this json data.
{ "created": 1326853478, "livemode": false, "id": "evt_00000000000000", "type": "invoice.payment_succeeded", "object": "event", "request": null, "pending_webhooks": 1, "api_version": "2017-06-05", "data": {
"object": {
"id": "in_00000000000000",
"object": "invoice",
"amount_due": 500,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"charge": "_00000000000000",
"closed": true,
"currency": "usd",
"customer": "cus_00000000000000",
"date": 1501950408,
"description": null,
"discount": null,
"ending_balance": 0,
"forgiven": false,
"lines": {
"data": [
{
"id": "sub_BLXcTSekdHflq5",
"object": "line_item",
"amount": 500,
"currency": "usd",
"description": null,
"discountable": true,
"livemode": true,
"metadata": {
},
"period": {
"start": 1507246341,
"end": 1509924741
},
"plan": {
"id": "Subscriber",
"object": "plan",
"amount": 500,
"created": 1501901993,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "Subscriber",
"statement_descriptor": "WatchBuddie Stream sub",
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": null,
"subscription_item": "si_1AyqWnFr5iCt1Tv7n23zDLOM",
"type": "subscription"
}
],
"total_count": 1,
"object": "list",
"url": "/v1/invoices/in_1AnV6yFr5iCt1Tv7PnqZ0EUA/lines"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"paid": true,
"period_end": 1501950408,
"period_start": 1501950408,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"subscription": "sub_00000000000000",
"subtotal": 500,
"tax": null,
"tax_percent": null,
"total": 500,
"webhooks_delivered_at": 1501950409
} } }
webhook method
def webhooks
begin
event_json = JSON.parse(request.body.read)
event_object = event_json['data']['object']
#refer event types here https://stripe.com/docs/api#event_types
case event_json['type']
when 'invoice.payment_succeeded'
#Update the total subscription total
#Send in email to the user telling them that they resubbed
logger.debug event_object['lines']['id']
end
My question is how do I get the "id": "sub_BLXcTSekdHflq5" I tried
logger.debug event_object['lines']['id']
It doesn't seem to be working what am I doing wrong? I'm sure its just a syntax thing that I am not understanding.
Thanks for all the help!
Figured it out the lines data was in a hash so you had to iterate over it. This is what I did for anyone who comes across this.
event_object['lines']['data'].each{ |i|
sub_id = i['id']
logger.debug sub_id
}

how to handle the below data

The problwm what iam facing is ..when ever iam passing the values like array inside the dictionyy..it is showing me the error -1005 connection was lost..and when ever i dont pass the values the response is getting successfully created but inthe server side database the ofiice price details and office contact information and office document is not saving ..can any one help me how to handle thanks in advance
[
{
"OfficePrice": [
{
"office": 1,
"making_charge": 1,
"wastage": 1,
"weight_by": 1,
"credit_period": 1,
"cartage_price_type": null,
"cartage_price": null,
"diamond_price": 1,
"amethyst_price": 1,
"amber_price": 1,
"sapphire_price": 1,
"emerald_price": null,
"ruby_price": null,
"id": 1,
"createdAt": "2016-04-15T08:41:08.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
}
],
"OfficeContactPerson": [
{
"office": 1,
"contact_name": null,
"designation": "ASDF",
"department": "234",
"mobile": "1231231231",
"email": null,
"incharge_status": null,
"created_by": null,
"modified_by": null,
"id": 1,
"createdAt": "2016-04-15T08:41:08.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
},
{
"office": 1,
"contact_name": null,
"designation": "ASDF",
"department": "ASDF",
"mobile": "1231231231",
"email": null,
"incharge_status": null,
"created_by": null,
"modified_by": null,
"id": 2,
"createdAt": "2016-04-15T08:41:08.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
}
],
"OfficeBank": [
{
"office": 1,
"bank": 2,
"branch": null,
"city": null,
"account_type": 1,
"ifsc_code": "12123",
"account_number": "12234",
"account_name": "asdfasd",
"id": 3,
"createdAt": "2016-04-16T14:18:39.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
}
],
"OfficeDocument": [
{
"office": 1,
"document_original": null,
"document_stored_name": null,
"id": 1,
"createdAt": "2016-04-15T08:41:08.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
}
],
"city": {
"name": "Bengalore",
"id": 1,
"createdAt": null,
"updatedAt": "2016-04-16T16:02:53.000Z"
},
"state": {
"name": "Karnataga",
"id": 1,
"createdAt": null,
"updatedAt": "2016-04-16T16:02:53.000Z"
},
"district": {
"name": "BGL",
"id": 1,
"createdAt": null,
"updatedAt": "2016-04-16T16:02:53.000Z"
},
"name": "kkk",
"short_name": "asdf",
"shop_no": "1",
"door_no": "11",
"floor": "1",
"building": "1",
"street": "1",
"area": "1",
"main": "1",
"pincode": "1",
"telephone": "1",
"hallmarking": null,
"payment_by": null,
"delivered_to": null,
"active_status": null,
"delete_status": null,
"created_by": null,
"modified_by": null,
"office_photo_original": null,
"office_photo_stored_name": null,
"cin_path": null,
"vat": null,
"pan": null,
"others": "123123",
"id": 1,
"createdAt": "2016-04-15T08:41:08.000Z",
"updatedAt": "2016-04-16T14:18:39.000Z"
}
the dictionary can't save null values.
The NSDictionay format doesn't support null vales. Thats why its returning error.
The only possible solution for this is to update server database ,change null values to 0 or "NULL"(in double quotes). it will solve your issue.

Stripe response manipulation in Grails

Hi i have the below response for Stripe in my Grails 2.5.1 application , how i can read and manipulate it using Grails .
Response :
<com.stripe.model.Charge#1467160391 id=ch_17bcduFKvYH0sAt4OMSKjOzA> JSON: { "amount": 7200, "created": 1454790414, "currency": "egp", "id": "ch_17bcduFKvYH0sAt4OMSKjOzA", "livemode": false, "paid": true, "refunded": false, "disputed": null, "captured": true, "description": "customer#sample.org", "statement_description": null, "failure_message": null, "failure_code": null, "amount_refunded": 0, "customer": null, "invoice": null, "refunds": { "data": [], "total_count": 0, "has_more": false, "url": "/v1/charges/ch_17bcduFKvYH0sAt4OMSKjOzA/refunds", "count": null }, "card": null, "dispute": null, "balance_transaction": "txn_17bcduFKvYH0sAt4qSSDZ7Bj", "metadata": {} }

How do you grab a key/value pair from an array of hashes in ruby?

I have an array of hashes (or atleast I think they are hashes) and I need to pull out the ID for each of them. I'm sure ruby has some quick way of doing this ... I just can't figure it out.
I don't want to iterate through the arrays and build a new one.
[
[
{
"bio": "I am a tech geek who loves starting up companies. While I was in college, I founded Squeeze My Tees",
"business_name": "Rounded Development",
"city": "",
"created_at": "2012-04-22T18:07:44Z",
"first_name": "Brian",
"id": 1,
"industry": "Entertainment",
"last_name": "Weinreich",
"lat": null
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:35Z",
"email": "test#jambo.com",
"first_name": "asdad",
"id": 2,
"industry": null,
"last_name": "ddfs",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:35Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:39Z",
"email": "tesasdat#jambo.com",
"first_name": "fdsd",
"id": 3,
"industry": null,
"last_name": "asdgw",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:39Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:44Z",
"email": "asdsad#jambo.com",
"first_name": "ewtrwef",
"id": 4,
"industry": null,
"last_name": "dfd",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:44Z"
}
]
]
To pull out just the IDs you can do this:
the_IDs = array_of_hashes.collect { |single_array| single_array["id"] }
Obviously you can use less verbose variable names, they're just for illustration. But the idea is that you can loop through an array and collect whatever the block returns. In this case, you keep getting an ID returned, and the_IDs will just be an array of what was collected.

Resources