RestKit how to map an array inside an array - ios

Here's the response:
{
"status": true,
"statuscode": 200,
"result": [
{
"name": "ABC",
"date": "2015-01-30",
"documents": [
{
"id": 1,
"name": "doc1",
"status": "complete",
},
{
"id": 2,
"name": "doc2",
"status": "complete",
},
{
"id": 3,
"name": "doc3",
"status": "complete",
}
],
"message": "Hello World",
"status": 3
}
]
}
I want to map and get only all the "document" inside an array keyed "result" and I don't need anything with other objects / mappings. I just need the documents. How can that be done / declared in the response descriptors to automatically match all these documents to my managed object?

Try This :-
NSDictionary *dic=#{
#"status": #true,
#"statuscode":# 200,
#"result": #[
#{
#"name": #"ABC",
#"date": #"2015-01-30",
#"documents": #[
#{
#"id":# 1,
#"name": #"doc1",
#"status": #"complete",
},
#{
#"id":# 2,
#"name": #"doc2",
#"status":# "complete",
},
#{
#"id":# 3,
#"name": #"doc3",
#"status": #"complete",
}
],
#"message": #"Hello World",
#"status":# 3
}
]
};
I am storing your response in NSDictionary and get it by -
[[[dic objectForKey:#"result"] objectAtIndex:0] objectForKey:#"documents"]
change your indexNumber as require !

NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; //From Server you will get response data in form of NSData , so the 'responseData' is a type of NSData
NSArray *appDetais = [jsonData objectForKey:#"result"];
NSDictionary *resultJsonData = [appDetais objectAtIndex:0];
NSArray *documentDetailsArray = [jsonData resultJsonData:#"documents"];
for(int i=0;i<[documentDetailsArray count];i++){
NSDictionary *singleDocumentDetail = [documentDetailsArray objectAtIndex:0];
NSLog(#"%#",[singleDocumentDetail objectForKey:#"id"]);
}
You may try this.. :)

Related

IOS JSON Parsing Containing Multiple Arrays

I have a JSON array with multiple object and I don't know how do I grab the "url" tag as an NSArray or a NSDictionary and show that image url in CollectionView. I can't change the JSON data format.How should I do this?
Here is what the JSON response looks like:
{
"error": false,
"data": [
{
"albumid": 2,
"albumtitle": "Album 2",
"images": [
{
"image": "Img2.jpeg",
"imageid": 11
},
{
"image": "vr4.jpg",
"imageid": 4
},
{
"image": "3purple b-ball.jpg",
"imageid": 3
}
]
},
{
"albumid": 3,
"albumtitle": "Album 3",
"images": [
{
"image": "vr2.jpg",
"imageid": 6
},
{
"image": "vr1.jpg",
"imageid": 5
}
]
},
{
"albumid": 4,
"albumtitle": "Album 4",
"images": [
{
"image": "vr1.jpg",
"imageid": 8
}
]
},
{
"albumid": 12,
"albumtitle": "My Album",
"images": [
{
"image": "img3.jpeg",
"imageid": 64
},
{
"image": "img4.jpeg",
"imageid": 63
},
{
"image": "img5.jpeg",
"imageid": 62
}
]
},
{
"albumid": 13,
"albumtitle": "Demo Album",
"images": [
{
"image": "img6.jpeg",
"imageid": 67
},
{
"image": "img7.jpeg",
"imageid": 66
},
{
"image": "img11.jpeg",
"imageid": 65
}
]
}
]
}
Try something like this (data is the NSData object you get from your webserver):
NSError *jsonError = nil;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if(!jsonError){
NSArray *dataArray = [jsonData objectForKey:#"data"];
NSDictionary *albumDict = [dataArray objectAtIndex:0];
NSArray *imagesArray = [albumDict objectForKey:#"images"];
NSDictionary *imageDict = [imagesArray objectAtIndex:0];
NSString *imageURL = [imageDict objectForKey: #"image"];
}
Of course, you have to use loops for the array elements, this example is only with element 0. Some more checks if a field exists etc. would be good.
You can create a model to hold the image id and url. You add an array of these image objects in the main model (Album). In Swift, your models might look like this
class Album {
var albumId = ""
var albumTitle = ""
var albumImages = [AlbumImage]()
}
class AlbumImage {
var imageId = ""
var imageUrl = ""
}
Hope this helps!
Use this code after downloaded the json file.
NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *rootArray = rootDict[#"data"];
NSDictionary *subDict;
NSArray *subArray;
NSDictionary *tempDict;
for (int i=0; i<rootArray.count; i++) {
subDict = rootArray[i];
subArray = subDict[#"images"];
for (int j=0; j<subArray.count; j++) {
tempDict = subArray[j];
NSLog(#"%#",tempDict[#"image"]);
}
}
in JSONObjectSerialization data was NSData object where the json file downloaded.

Nested NSDictionary from JSON in Xcode

Hi I been having huge issues grabbing some data out of this for me quite complex JSON structure.
The problem is that the JSON structure contains some nesting that i can't find out how to handle.
Example of the JSON i get from the database:
{
"hits": {
"totalHits": 3202,
"hits": [{
"id": "70132eb7-2834-458c-900a-da951c95a506",
"versions": [{
"id": 7,
"properties": {
"Status": [
"usable"
],
"created": [
"2015-10-27T14:31:13Z"
],
"Text": [
"Snabbtåg, Järnväg, Höghastighetsjärnväg, "
],
"ConceptDefinitionLong": [
"Enligt Trafikverket saknas en helt entydig och vedertagen definition av höghastigheteståg."
],
"contenttype": [
"Concept"
],
"ConceptProposalUser": [
"[object Object]"
],
"ConceptType": [
"object"
],
"Name": [
"Höghastighetståg"
],
"ConceptNote": null,
"ConceptSeeAlso": null,
"Note": null,
"ConceptName": [
"Höghastighetståg"
],
"updated": [
"2016-02-01T11:37:30Z"
],
"ConceptDefinitionShort": [
"Snabbtåg, Järnväg, Höghastighetsjärnväg, Kollektivtrafik"
],
"ConceptStatus": [
"usable"
]
}
}],
"noVersions": 1
}, {
"id": "4224ccfb-1f0a-9491-727f-f6ab0fc2c951",
"versions": [{
"id": 2,
"properties": {
"Status": [
"usable"
],
"created": [
"2016-01-25T12:03:33Z"
],
"Text": [
"Rosenlundsbadet öppnade 1968 och äventyrsbadet 1991."
],
"ConceptDefinitionLong": [
"Rosenlundsbadet är en badanläggning i Jönköping. "
],
"contenttype": [
"Concept"
],
"ConceptProposalUser": null,
"ConceptType": [
"organisation"
],
"Name": [
"Rosenlundsbadet"
],
"ConceptNote": null,
"ConceptSeeAlso": null,
"Note": null,
"ConceptName": [
"Rosenlundsbadet"
],
"updated": [
"2016-01-25T12:03:38Z"
],
"ConceptDefinitionShort": [
"Simning, Simhopp, Äventyrsbad"
],
"ConceptStatus": [
"usable"
]
}
}],
"noVersions": 1
}
...
My job is to get all the "Name"s from all posts where the "Status" is set to "usable". And store them in my app. But I'm having a hard time getting that info . Im new to JSON and can't handle the structure. This is what i got so far after hours of googling:
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
JSONObjectWithData:allCoursesData
options:kNilOptions
error:&error];
if( error )
{
NSLog(#"%#", [error localizedDescription]);
}
else {
NSArray* entries = [JSONdictionary valueForKeyPath:#"hits.hits"];
NSDictionary *firstItem = [entries objectAtIndex:0];
NSString *id1 = [firstItem objectForKey:#"id"];
NSLog(#"ID: ");
NSLog(id1);
}}
From that code I'm able to get the ID from the first item. But I don't seem to be able to fetch the "name" or check if they are "usable". Have been working with this and trying to solve this for hours but this is above my leauge. I guess the reason to why I can't solve it is cause the data is nested in hits.hits etc. Yet I have to solve this to be able to finish my app. I would be very grateful for some help.
Here is a longer version of the JSON database. The full version contains over 3000 items:
http://jsonviewer.stack.hu/#http://www.pumba.se/example.json
You can use the following code:
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
JSONObjectWithData:allCoursesData
options:kNilOptions
error:&error];
if( error )
{
NSLog(#"%#", [error localizedDescription]);
}
else {
NSMutableArray *allNames = [NSMutableArray array];
NSArray* entries = [JSONdictionary valueForKeyPath:#"hits.hits"];
for (NSDictionary *hit in entries) {
NSArray *versions = hit[#"versions"];
for (NSDictionary *version in versions) {
NSDictionary *properties = version[#"properties"];
NSString *status = [properties[#"Status"] firstObject];
NSString *name = [properties[#"Name"] firstObject];
if ([status isEqualToString:#"usable"]) {
[allNames addObject:name];
}
}
}
NSLog(#"All names: %#", allNames);
}}

Pulling Data from an NSDictionary

So I have an NSDictionary that has a variety of data within it. When printed to the log, it prints like this:
[{"user_id":3016817,"grade":"A","percent":"93","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":95.0,"percent":"93","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":90.0,"percent":"93","comment":null,"id":3268181}],"grade":"A","percent":"93","name":"Default"}]},{"user_id":3016818,"grade":"A","percent":"94","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":92.0,"percent":"94","comment":null,"id":3268180},{"points":100.0,"grade":"A","score":95.0,"percent":"94","comment":null,"id":3268181}],"grade":"A","percent":"94","name":"Default"}]}]
If I use a formatter online, its a lot more readable and looks something like this:
[
{
"user_id": 3016817,
"grade": "A",
"percent": "93",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "93",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 90,
"percent": "93",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "93",
"name": "Default"
}
]
},
{
"user_id": 3016818,
"grade": "A",
"percent": "94",
"grading_periods": [
{
"assignments": [
{
"points": 100,
"grade": "A",
"score": 92,
"percent": "94",
"comment": null,
"id": 3268180
},
{
"points": 100,
"grade": "A",
"score": 95,
"percent": "94",
"comment": null,
"id": 3268181
}
],
"grade": "A",
"percent": "94",
"name": "Default"
}
]
}
]
My question would be how would I access the value of grade or score for a specific user_id using this dictionary?
Your string represents a NSArray, not a NSDictionary. And it's a JSON string, so you can parse it using NSJSONSerialization:
NSString *jsonString = #"..." // your string here
// Create array from json string
NSArray *jsonArray = [NSJSONSerialization
JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:Nil];
// Loop to find your user_id
// Because each child of this array is a dictionary
for (NSDictionary *dic in jsonArray) {
if ([dic[#"user_id"] isEqual:#3016817]) { // user_id field is number
// Access what you want
NSString *grade = dic[#"grade"];
// For "score" you must go deeper
// Just remember, [] is array and {} is dictionary
}
}
A simple way to do this would be
for (NSDictionary* d in theArray) {
if ([d[#"user_id"] isEqualToString: u]) {
// do something
}
}
And matt is right, it is an array of dictionaries, no matter what type you declared to be.
You can use below class method of NSJsonSerialization class to create NSArray which will contain all the dictionaries.
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
Once you get the array of dictionaries from JSON, you can do following:
NSArray *filteredjsonArr = [jsonArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"user_id == %#", #"3016818"]];
NSDictionary *dict = [filtered firstObject];
if (dict != nil) {
NSString *grade = [dict objectForKey:#"grade"];
NSArray *gradingPeriods = [dict objectForKey:#"grading_periods"];
}
To access score and grade for specific assignments, you'll need to drill down further into gradingPeriods array.

How get the value from NSDictionary with JSON

So I have this json
data: {
Thresh: {
id: 412,
key: "Thresh",
name: "Thresh",
title: "the Chain Warden"
},
Aatrox: {
id: 266,
key: "Aatrox",
name: "Aatrox",
title: "the Darkin Blade"
},
Tryndamere: {
id: 23,
key: "Tryndamere",
name: "Tryndamere",
title: "the Barbarian King"
}
I'm trying to get all "id", but the only way I can find is using
[myArray addObject:[dictionary valueForKeyPath:#"data.Aatrox.id"]];
I want to know if have a easy way to get all id's, like without need to put the name example "Aatrox", just "id" or "title".
Here the code that I use to get the json
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:#"myurl"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
Assuming your JSON looks like:
{
"data": {
"Thresh": {
"id": 412,
"key": "Thresh",
"name": "Thresh",
"title": "the Chain Warden"
},
"Aatrox": {
"id": 266,
"key": "Aatrox",
"name": "Aatrox",
"title": "the Darkin Blade"
},
"Tryndamere": {
"id": 23,
"key": "Tryndamere",
"name": "Tryndamere",
"title": "the Barbarian King"
}
}
}
If that's the case, then you can then grab the dictionary associated with the key of data, and then grab an array of all the values in that dictionary (i.e. an array of those child dictionaries):
NSArray *values = [dictionary[#"data"] allValues];
And then grab the array of values associated with the id key in each of those dictionaries:
NSArray *ids = [values valueForKeyPath:#"id"];
You can use NSString class - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator method to separate your JSON object to pieces of strings. After that you can use a for loop to get elements that you need.(This is such a nasty way, but accomplishes what you asked) However, I strongly recommend you to read JSON string as an object by using Jastor like library and by creating your own objects. Maybe this way seems to be hard, but it will be safer than your implementation.
Lets just say your JSON looks like this:
{
"Thresh": {
"id": 412,
"key": "Thresh",
"name": "Thresh",
"title": "the Chain Warden"
},
"Aatrox": {
"id": 266,
"key": "Aatrox",
"name": "Aatrox",
"title": "the Darkin Blade"
},
"Tryndamere": {
"id": 23,
"key": "Tryndamere",
"name": "Tryndamere",
"title": "the Barbarian King"
}
}
and you parse it using
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
You can use KVC collection operations to get exactly what you want. First we need to convert your dictionary to an array of values. Since you don't care about the keys this is easy:
NSArray *values = [dictionary allValues];
Now we want all the IDs so we need to use #distinctUnionOfObjects to get that:
NSArray *ids = [values valueForKeyPath:#"#distinctUnionOfObjects.id"];
We can combine this into a single line if we want:
NSArray *ids = [dictionary valueForKeyPath:#"allValues.#distinctUnionOfObjects.id"];
You can read more about KVC collection operations here: http://nshipster.com/kvc-collection-operators/
I would also suggest using a higher level JSON parsing library such as RestKit.

Parsing a response from JSON , ios?

I have the below parameter in my json.
{
"msg": "success",
"data": [
{
"FNAME": "test",
"LNAME": null,
"STATUS": null,
"MOBILE1": "1234567890",
"show_email": "1",
"Info": [
{
"id": "73307",
"NAME": "demo",
"CONTACT": "",
"WORKING_HOUR1": "[\"09:00 AM\",\"09:15 AM\",\"09:30 AM\",\"09:45 AM\",\"10:00 AM\"]",
"WORKING_HOUR7": "",
"DAY": "[\"Monday\",\"Wednesday\"]"
}
]
}
]
}
I am not able to understand how do I get values from it.
If I parse this I get the error
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *results = [res objectForKey:#"data"];
NSArray *Info=[results[0] objectForKey:#"Info"];
NSArray *day=clinicInfo[1][#"DAY"];
NSLog(#"%#", day[0]);
Error:
'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndexedSubscript:
EDIT:
From your JSON you want:
NSArray *days = json[#"data"][0][#"Info"][0][#"DAY"]
Also while your JSON is valid, the days and working hours are not in an array - they are a string.
You need something like this.
{
"msg": "success",
"data": [
{
"FNAME": "test",
"LNAME": null,
"STATUS": null,
"MOBILE1": "1234567890",
"show_email": "1",
"Info": [
{
"id": "73307",
"NAME": "demo",
"CONTACT": "",
"WORKING_HOUR1": [
"09:00 AM",
"09:15 AM",
"09:30 AM",
"09:45 AM",
"10:00 AM"
],
"WORKING_HOUR7": "",
"DAY": [
"Monday",
"Wednesday"
]
}
]
}
]
}
Firstly you are not using valid JSON.
{
"DAY": [
"Monday",
"Wednesday"
]
}
You can use NSJSONSerialization to parse the JSON file, from which you should get a NSDictionary.
In that dictionary there should be a NSArray for the key "DAY", which contains 2 objects both strings, "Tuesday" and "Thursday".
eg. Where data is your JSON file
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if(!jsonError) NSArray *days = json[#"DAY"];
else NSLog(#"Error serialising JSON");

Resources