How to parse json ki typical format in iphone - 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"];

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.

Elasticsearch nested query

I'm new to elasticsearch, managed to set it up and import recordset from my mongodb collection using the river plugin. For a start, I want to query against the "desc" field but just can't manage to get the query .. not sure if the problem is driven by the way index was defined.. can anyone help please?
Sample recordset in elastic search looks like this
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 107209,
"max_score": 1,
"hits": [
{
"_index": "shiv",
"_type": "shiv",
"_id": "iG1eIzN7RGO7hFfxTlnLuA",
"_score": 1,
"_source": {
"_id": {
"$oid": "50901d7f485bf7bd1c000021"
},
"brand": "",
"category": {
"$ref": "categories",
"$id": {
"$oid": "4fbd2221758cb11d14000174"
}
},
"comments": [],
"count_comment": 0,
"count_fav": 2,
"count_hotness": 1.46,
"count_rekick": 0,
"count_share": 0,
"country": {
"$ref": "countries",
"$id": {
"$oid": "4fec98f7758cb18c6e0002c9"
}
},
"currency": "pound",
"desc": "A men's automatic watch, this Seamaster Bond model features a Co-Axial escapement and date function. Its blue dial is teamed with a stainless steel case and bracelet for a look that's sporty and refined.",
"gender": "male",
"ident": "omega-seamaster-diver-bond-men-s-automatic-watch---ernest-jones-1351622015",
"img_url": "http://s7ondemand4.scene7.com/is/image/Signet/5735793?$detail$",
"lifestyles": [
{
"$ref": "lifestyles",
"$id": {
"$oid": "508ff6ca485bf73112000060"
}
}
],
"location": "United Kingdom",
"owner": {
"$ref": "accounts",
"$id": {
"$oid": "50742fd8485bf74b7a00213f"
}
},
"price": 2400,
"store": "ernestjones.co.uk",
"tags": [
"ernest-jones",
"bond"
],
"timestamp_creation": 1351622015,
"timestamp_exp": 1356825600,
"timestamp_update": 1351622015,
"title": "Omega Seamaster Diver Bond men's automatic watch - Ernest Jones",
"url": "http%3A%2F%2Fwww.ernestjones.co.uk%2Fwebstore%2Fd%2F5735793%2Fomega%20seamaster%20diver%20bond%20men%27s%20automatic%20watch%2F%3Futm_source%3Dgooglebase%26utm_medium%3Dfeedmanager%26cm_mmc%3DFroogle-_-CKB-_-nurses_fobs-_-watches%26cm_mmca1%3Domega%26cm_mmca2%3Dmale%26cm_mmca3%3Dadult"
}
}
]
}
}
The mapping of the index "shiv" looks like
{
"shiv": {
"properties": {
"$oid": {
"type": "string"
}
}
}
}
Thanks again
There are lots of ways to query, have you tried a match query?
Using curl or a rest client of your choice...
http://[host]:9200/[index_name]/[doc_type]/_search
{
"query" : {
"match" : {
"desc" : "some value you want to find in desc"
}
}
}

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

IOS 5 JSON Parsing with sub level

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. :)

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 ..
}