IOS 5 JSON Parsing with sub level - iphone

I Try to parse a Json feed with IOS 5.
My Json File is like that :
{
"status": "ok",
"count": 18,
"count_total": 2248,
"pages": 125,
"posts": [
{
"id": 31781,
"type": "post",
"slug": "aaa",
"url": "http:\/\/www.example.com\/videos\/aaa.html",
"status": "publish",
"title": "my Title",
"title_plain": "My Title",
"content": "<p>Jdfkdfkjkjdfklfdkldfkldfklfkdld.<\/p>\n",
"excerpt": "Jdfkdfkjkjdfklfdkldfkldfklfkdlds.",
"date": "2012-01-26 07:38:29",
"modified": "2012-01-26 07:38:29",
"categories": [
{
"id": 4,
"slug": "videos",
"title": "Videos",
"description": "",
"parent": 0,
"post_count": 476
}
],
"tags": [],
"author": {
"id": 4,
"slug": "author",
"name": "Au Thor",
"first_name": "",
"last_name": "",
"nickname": "Au Thor",
"url": "",
"description": ""
},
"attachments": [
{
"id": 31784,
"url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
"slug": "primo",
"title": "primo",
"description": "",
"caption": "",
"parent": 31781,
"mime_type": "image\/jpeg",
"images": {
"full": {
"url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
"width": 620,
"height": 389
},
"thumbnail": {
"url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo-150x75.jpg",
"width": 150,
"height": 75
},
"medium": {
"url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
"width": 620,
"height": 389
},
"large": {
"url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
"width": 620,
"height": 389
}
}
}
],
"comment_count": 1,
"comment_status": "open",
"thumbnail": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo-150x75.jpg"
},
I have access to Title, content... like that :
NSArray* latestArticles = [json objectForKey:#"posts"];
NSDictionary* Article = [latestArticles objectAtIndex:0];
NSString *Titre = [Article objectForKey:#"title"];
But how I can have access to attachement > image > full > url field ?
I'm lost, and new with JSON...
Thanks for your help

Haven't testest but I believe it would be.
NSArray *allPosts = [json objectForKey:#"posts"];
NSDictionary *firstPost = [allPosts objectAtIndex:0];
NSArray *allAttachments = [firstPost objectForKey:#"attachments"];
NSDictionary *firstAttachment = [allAttachments objectAtIndex:0];
NSDictionary *allImages = [firstAttachment objectForKey:#"images"];
NSDictionary *fullImage = [allImages objectForKey:#"full"];
NSString *urlString = [fullImage objectForKey:#"url"];
With NSJSONSerialization if you see [] the object will be an NSArray, if you see {} the object will be an NSDictionary.

You can also get it by
NSArray *posts=json[#"posts"];
NSDictionary *newArticle=posts[0];
NSString *title=newArticle[#"title"];
Remember Objective-c is actually c language with object oriented concepts. You can apply basic c code or basic c syntax too. :)

Related

Strapi multi-level population with field selection

I want to populate a multilevel relational field on strapi with REST API with specific field selection, but only the id comes up at the response.
When I populate the field without field selections with a GET request at: localhost:1337/api/home?populate[0]=Reasons&populate[1]=Reasons.IconsList&populate[2]=Reasons.IconsList.Icon, I
get this JSON response:
{
"data": {
"id": 1,
"attributes": {
"createdAt": "2022-10-31T11:24:55.202Z",
"updatedAt": "2022-11-09T13:50:40.445Z",
"publishedAt": "2022-10-31T11:26:30.944Z",
"PageInfo": "Homepage",
"Reasons": {
"id": 1,
"IconsList": [
{
"id": 1,
"Content": "Free cancellation on most bookings",
"Icon": {
"data": {
"id": 3,
"attributes": {
"name": "cancellation.svg",
"alternativeText": "Free cancellation on most bookings",
"caption": "cancellation.svg",
"width": 21,
"height": 21,
"formats": null,
"hash": "cancellation_b2308d4b63",
"ext": ".svg",
"mime": "image/svg+xml",
"size": 1.57,
"url": "/uploads/cancellation_b2308d4b63.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"createdAt": "2022-10-20T12:02:37.735Z",
"updatedAt": "2022-10-31T11:23:48.326Z"
}
}
}
},
{
"id": 2,
"Content": "No credit card fees",
"Icon": {
"data": {
"id": 5,
"attributes": {
"name": "credit-card.svg",
"alternativeText": "credit-card.svg",
"caption": "credit-card.svg",
"width": 22,
"height": 16,
"formats": null,
"hash": "credit_card_06b9eca40f",
"ext": ".svg",
"mime": "image/svg+xml",
"size": 0.89,
"url": "/uploads/credit_card_06b9eca40f.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"createdAt": "2022-10-20T12:21:46.342Z",
"updatedAt": "2022-10-20T12:21:46.342Z"
}
}
}
},
{
"id": 3,
"Content": "24/7 Customer care",
"Icon": {
"data": {
"id": 2,
"attributes": {
"name": "customer-care.svg",
"alternativeText": "24/7 Customer care",
"caption": "customer-care.svg",
"width": 20,
"height": 22,
"formats": null,
"hash": "customer_care_6aa8ac8eea",
"ext": ".svg",
"mime": "image/svg+xml",
"size": 1.48,
"url": "/uploads/customer_care_6aa8ac8eea.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"createdAt": "2022-10-20T12:02:37.734Z",
"updatedAt": "2022-10-20T12:04:39.792Z"
}
}
}
}
]
}
}
},
"meta": {}
}
Following this guide: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html#combining-population-with-other-operators, when I try to define the fields I want from the response: localhost:1337/api/home?fields[0]=PageInfo&populate[Reasons][IconsList]fields]=Content, I only get this JSON:
{
"data": {
"id": 1,
"attributes": {
"PageInfo": "Homepage",
"Reasons": {
"id": 1
}
}
},
"meta": {}
}
I want to get IconsList's Content and Icon.alternativeText and Icon.url but I cannot find how to build my query parameters.
For future reference to this problem, my question was answered on Strapi forum: https://forum.strapi.io/t/strapi-multi-level-population-with-field-selection/23473
This tutorial explains the reasoning behind deep populating and filtering and it's really interesting.
The problem was that you need to pupulate the relational field again like that: localhost:1337/api/home?fields[0]=PageInfo&populate[Reasons][populate][IconsList][fields][0]=Content to be able to access its data.

How to parse json ki typical format in iphone

I am trying to parse json. but not able to get correct key. I am using JSONKIT. Below is the json data.SO i am trying to get CONTENT pls help me how to do this.
`{
"apiVersion": "2.1",
"data": {
"updated": "2013-06-13T06:15:22.613Z",
"totalItems": 1000000,
"startIndex": 1,
"itemsPerPage": 25,
"items": [
{
"id": "l2h9kFq3TFQ",
"uploaded": "2013-04-09T15:20:46.000Z",
"updated": "2013-06-11T22:32:56.000Z",
"uploader": "trollinforlaughs",
"category": "Games",
"title": "Funny Fail Compilation 2013",
"description": "by FAIL.nl LOOKING FOR YOUTUBE PARTNERSHIP? http://www.acifinnetwork.com/a/bigshotz \"Extra Tags: \"girls fail compilation\" \"fail compilation 2012\" \"compilatio...",
"thumbnail": {
"sqDefault": "https://i.ytimg.com/vi/l2h9kFq3TFQ/default.jpg",
"hqDefault": "https://i.ytimg.com/vi/l2h9kFq3TFQ/hqdefault.jpg"
},
"player": {
"default": "https://www.youtube.com/watch?v=l2h9kFq3TFQ&feature=youtube_gdata_player",
"mobile": "https://m.youtube.com/details?v=l2h9kFq3TFQ"
},
"content": {
"1": "rtsp://v8.cache7.c.youtube.com/CiILENy73wIaGQlUTLdakH1olxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"5": "https://www.youtube.com/v/l2h9kFq3TFQ?version=3&f=videos&app=youtube_gdata",
"6": "rtsp://v8.cache7.c.youtube.com/CiILENy73wIaGQlUTLdakH1olxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
},
"duration": 776,
"aspectRatio": "widescreen",
"rating": 4.463792,
"likeCount": "13094",
"ratingCount": 15121,
"viewCount": 6493167,
"favoriteCount": 0,
"commentCount": 5316,
"accessControl": {
"comment": "allowed",
"commentVote": "allowed",
"videoRespond": "moderated",
"rate": "allowed",
"embed": "allowed",
"list": "allowed",
"autoPlay": "allowed",
"syndicate": "allowed"
}
},
{
"id": "DfznjFrNvf4",
"uploaded": "2013-05-24T19:15:36.000Z",
"updated": "2013-06-10T01:11:01.000Z",
"uploader": "funnymediatv",
"category": "Comedy",
"title": "The Ultimate Fail,Win and Funny pranks Mega Compilation part 96",
"description": "Rate, Share and Subscribe ! Like us on Facebook: https://www.facebook.com/BestFunnyFails Funny Fails Community: https://plus.google.com/u/0/communities/11109...",
"thumbnail": {
"sqDefault": "https://i.ytimg.com/vi/DfznjFrNvf4/default.jpg",
"hqDefault": "https://i.ytimg.com/vi/DfznjFrNvf4/hqdefault.jpg"
},
"player": {
"default": "https://www.youtube.com/watch?v=DfznjFrNvf4&feature=youtube_gdata_player",
"mobile": "https://m.youtube.com/details?v=DfznjFrNvf4"
},
"content": {
"1": "rtsp://v6.cache2.c.youtube.com/CiILENy73wIaGQn-vc1ajOf8DRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"5": "https://www.youtube.com/v/DfznjFrNvf4?version=3&f=videos&app=youtube_gdata",
"6": "rtsp://v6.cache2.c.youtube.com/CiILENy73wIaGQn-vc1ajOf8DRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
},
"duration": 619,
"aspectRatio": "widescreen",
"rating": 4.6666665,
"likeCount": "220",
"ratingCount": 240,
"viewCount": 55915,
"favoriteCount": 0,
"commentCount": 28,
"accessControl": {
"comment": "allowed",
"commentVote": "allowed",
"videoRespond": "moderated",
"rate": "allowed",
"embed": "allowed",
"list": "allowed",
"autoPlay": "allowed",
"syndicate": "allowed"
}
}
]
}
}`
SO i am trying to get CONTENT pls help me how to do this.
Sample Code :
NSDictionary *content = [[[[yourJSONResponseDict objectForKey:#"data"] objectForKey:#"items"] objectAtIndex:0] objectForKey:#"content"];
NSLog(#"Content :: %#",content);
Explanation :
//1. This will give you the "Value" for key "data".
NSDictionary *data = [yourJSONResponseDict objectForKey:#"data"];
//2. This will give you the "Value" for key "items" which is inside "data" Dictionary.
NSArray *items = [data objectForKey:#"items"];
//3. This will give you the First Object of "items" Array which is a Dictionary.
NSDictionary *itemObject = [items objectAtIndex:0];
//4. This will give you your Final Component named "Content" which is a Dictionary also.
NSArray *content = [itemObject objectForKey:#"content"];

How can I get all photos in one post using Graph API?

{
"id": "11882030_4952296803730",
"from": {
"name": "xxx",
"id": "11882030"
},
"message": "test",
"picture": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/408410_4952294483672_298434229_s.jpg",
"link": "https://www.facebook.com/photo.php?fbid=4952294483672&set=pcb.4952296803730&type=1&relevant_count=2",
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yx/r/og8V99JVf8G.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/11882030/posts/4952296803730"
},
{
"name": "Like",
"link": "https://www.facebook.com/11882030/posts/4952296803730"
}
],
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"place": {
"id": "471607792876974",
"name": "TTT",
"location": {
"street": "",
"zip": "",
"latitude": x,
"longitude": x
}
},
"type": "photo",
"status_type": "mobile_status_update",
"object_id": "4952294483672",
"application": {
"name": "Facebook for iPhone",
"namespace": "fbiphone",
"id": "6628568379"
},
"created_time": "2013-01-17T01:29:59+0000",
"updated_time": "2013-01-17T01:29:59+0000",
"comments": {
"count": 0
}
}
As above, I posted a status with two photos. I can get the first photo's thumb URL in picture and the relevant link & count information in link.
But how can I get each photo's specific URL?
FQL often provides more information than Graph API. You have to use the attachment parameter of the stream FQL table to get all the attached photos.
SELECT attachment FROM stream
WHERE source_id = me()
AND post_id="11882030_4952296803730"
Result:
{
"data": [
{
"attachment": {
"media": [
{
"href": "https://www.facebook.com/photo.php?fbid=471082296...",
"alt": "",
"type": "photo",
"src": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash3/5826...",
"photo": {
"aid": "4391039135",
"pid": "439104482145",
"fbid": 471507,
"owner": 102832,
"index": 1,
"width": 485,
"height": 172,
"images": [
{
"src": "https://fbcdn-photos-a.akamaihd.net/hph...",
"width": 130,
"height": 46
}
]
}
}
],
"name": "",
"caption": "",
"description": "",
"properties": [
],
"icon": "https://fbstatic-a.akamaihd.net/rsrc.phpjk.gif",
"fb_object_type": "album",
"fb_object_id": "4391044992857139135"
},
{
... //Photo 2
}
}
]
}
Based on Stéphane Bruckerts answer; you're after attachments. So I'd recommend simply requesting the attachments field in your graph request.
Although this question is a few years old, it still comes up when searching for this topic.
Facebook (Meta) Graph API has changed. Current Facebook (Meta) Graph API is version 13.0.
The best endpoint I found was to use the "/feed" URI slug, with "fields=attachments". Simply cURL / GET:
https://graph.facebook.com/v13.0/$PAGEID/feed?access_token=$AUTHTOKEN&fields=attachments,full_picture,id,message,story
Reference: https://developers.facebook.com/docs/graph-api/reference/v13.0/page/feed

Problems with parsing JSON feed in app

The following JSON feed contains orders.
How can I retrieve each order, when it doesn't contain a key (can´t use objectForKey)? Between the opening bracket and the first curly bracket there is no key.
[ <-----
{ <-----
"person": {
"address": "My road",
"city": "My City",
"name": "My Name",
},
"orderDate": "30.05.2012 10:27",
"orderId": 10001,
"orderlines": [
{
"unit": {
"unitId": "RA005",
"name": "My Unit name",
},
"id": 10007,
"deliveryInfo": {
"count": 1,
"date": "30.05.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 3,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
},
{
"person": {
"address": "My road 2",
"city": "My City 2",
"name": "My Name 2",
},
"orderDate": "30.04.2012 10:27",
"orderId": 10002,
"orderlines": [
{
"unit": {
"unitId": "RA006",
"name": "My Unit name 2",
},
"id": 10008,
"deliveryInfo": {
"count": 2,
"date": "01.06.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 2,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
}
]
whenever you see [] (square brackets), it represents array. so use object at index to return the at indexes. one approach is to pass the object into array like
NSMutableArray *arr = (NSMutableArray*) jsonData;
since the root of your json is an array,
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(#"Item: %#", item);
}
}

How to manage Facebook api response data

I am developing an app for iPhone and I am sending this url to facebook:
https://graph.facebook.com/search?type=checkin&access_token=ACCESS_TOKEN
and the facebook reply something like this:
{
"data": [
{
"id": "2081746282701",
"from": {
"name": "USER1",
"id": "xxxxxxxxxx"
},
"place": {
"id": "xxxxxxxxx",
"name": "\u03a0\u03b1\u03bb\u03bb\u03ac\u03b4\u03b9\u03bf",
"location": {
"city": "xxxxxxxx",
"country": "Greece",
"latitude": xxxxx,
"longitude":xxxxx
}
},
"application": {
"name": "Facebook for iPhone",
"id": "6628568379"
},
"created_time": "2011-05-25T01:22:00+0000",
"likes": {
"data": [
{
"id":"xxxxxx",
"name": TaggedUsers"
},
{
"id": "xxxxxx",
"name": "tagged user"
}
]
}
},
{
"id": "xxxxxxx",
"from": {
"name": "UserWhich puplish this",
"id": "740646984"
},
"tags": {
"data": [
{
"name": "XXXXXX",
"id": "xxxxxxxxxx"
}
]
},
"place": {
"id": "xxxxxxxxxxxxx",
"name": "xxxxxxxx",
"location": {
"latitude": xxxxxx,
"longitude": xxxxxx
}
},
"application": {
"name": "Facebook for Android",
"id": "350685531728"
},
"created_time": "2011-05-23T17:18:54+0000",
"likes": {
"data": [
{
"id": "xxxxxxx",
"name": "xxxxxxxxx"
}
]
}
}
}
My question is how I will manage this string to export long lat place and the user who checked in? I think that NSCaseInsensitiveSearch wont do the trick because there is no unique variable to search for. For example the "data" is placed at the start of the responsed string and before of each check in but its placed and before tagged user!
This is JSON: JavaScript Object Notation.
You can decode it easily into a NSDictionary or NSArray with the json-framework depending on the root element. After that it's a tree like structure depending on the JSON returned.
The mentioned framework provides an easy to use category for NSObject which offers the method JSONValue.
Example to decode the json you'v received:
NSString *myJson = #"...";
NSDictionary *decodedJson = [myJson JSONValue];
Now you can use fast enumeration or direct access(objectForKey:) to retrieve the data you are interested in.
NSArray *users = [decodedJson objectForKey:#"data"];
foreach(NSDictionary *user in users =) {
NSLog(#"user's id: %#", [user objectForKey:#"id"]);
NSDictionary *fromData = [user objectForKey:#"from"]);
NSLog(#"user is from: %#", [fromData objectForKey:#"name"]);
NSLog(#"user is from id: %#", [fromData objectForKey:#"id"]);
// and so on ..
}