I am developing an iOS app. I am getting JSON response, how do I Parse it? It is comprising of two different buttons data. How do I count it and show on buttons details?
JSON Response:
{
0 = {
date = "0000-00-00";
"is_active" = 1;
project = "New project";
"project_desc" = "New project";
"project_id" = 3;
time = "<null>";
"user_id" = 6;
};
1 = {
date = "2017-01-04";
"is_active" = 1;
project = "New Project 2";
"project_desc" = "New Project 2";
"project_id" = 4;
time = "06:29:54";
"user_id" = 6;
};
status = 1;
}
Parsing Code:
- (void)serverDidReceiveResponse:(NSDictionary *)response{
NSLog(#"%#",response);
for (NSDictionary *projectValue in response) {
_projectName = [projectValue valueForKey:#"time"];
_active = [projectValue valueForKey:#"is_active"];
}
for(NSDictionary*projectvalue in response)
{
for(NSDictionary *projectvalue1 in projectvalue)
{
_projectName = [projectValue1 valueForKey:#"time"]; _active = [projectValue1 valueForKey:#"is_active"];
}
}
Related
JSON OUTPUT:
category = (
{
"category_id" = 1;
column = 0;
description = "";
image = "images/Car Wash.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Car Wash";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
top = "";
},
{
"category_id" = 2;
column = 0;
description = "";
image = "images/Pest Control.png";
"meta_description" = "";
"meta_keyword" = "";
name = "Pest Control";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 3;
column = 0;
description = "";
image = "images/Electrician.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = Electrician;
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 4;
column = 0;
description = "";
image = "images/Washing Machine.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Washing Machine";
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 5;
column = 0;
description = "";
image = "images/TV Repair.png";
"meta_description" = "";
"meta_keyword" = "";
name = "TV Repair";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
success = 1;
}
MY CODE:
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
subCategory = [[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory==nil||[subCategory isEqual:#""])
{
productScreen *screen = [[productScreen alloc]init];
screen = [self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car = [[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
Here I have mentioned my code properly,getting the subcategory values has been done and I have comparing the values is null or the string is empty,but only else part is working what I have to do please any one help me.
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
NSArray *subCategory=[[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory.count ==0)//subcategory is an array
{
productScreen *screen=[[productScreen alloc]init];
screen =[self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car=[[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
You have given && condition inside if statement. Both conditions can't be true simultaneously. Moreover, check your string with isEqualToString method.
So put your if condition as given below -
if([subcategory length] ==0){
// Your code goes here.
}
I've managed to export the below data, multiplied by several hundred, into an NSDictionary. I now need to build out a way to put this data into Tinder style app. Can anyone advise how I can get my app to pull out the data against Key "Title" for various indexes. Currently it pulls out 45 records of data containing that one Title as there are 45 keys in my data set.
{
AttachmentFiles = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/AttachmentFiles";
};
};
Attachments = 0;
AuthorId = 22;
AverageRating = "<null>";
BestAnswerId = "<null>";
Body = "We need to think xyz";
CategoriesLookupId = 16;
Community = "<null>";
ContentType = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/ContentType";
};
};
ContentTypeId = 0x01200200C5BB1FEE2601B9439CD527C288D85A7B;
Created = "2014-03-21T08:37:33Z";
DescendantLikesCount = 8;
DescendantRatingsCount = "<null>";
DiscussionLastUpdated = "2014-06-19T12:57:58Z";
EditorId = 96;
EmailSender = "<null>";
FieldValuesAsHtml = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesAsHtml";
};
};
FieldValuesAsText = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesAsText";
};
};
FieldValuesForEdit = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FieldValuesForEdit";
};
};
File = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/File";
};
};
FileSystemObjectType = 1;
FilterCategory = "<null>";
FirstUniqueAncestorSecurableObject = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/FirstUniqueAncestorSecurableObject";
};
};
Folder = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/Folder";
};
};
GUID = "28144fd4-84b0-434b-9e99-01d939e6a773";
Has20likes = "<null>";
ID = 3;
Id = 3;
IsFeatured = 0;
IsIdeaCreated = "<null>";
IsQuestion = 0;
LastReplyById = 49;
LikesCount = 5;
MemberLookupId = 7;
Modified = "2015-03-11T15:25:20Z";
"OData__UIVersionString" = "2.0";
ParentItemEditorId = "<null>";
ParentItemID = "<null>";
ParentList = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/ParentList";
};
};
Popularity = "7.23632608186099";
RatingCount = "<null>";
RoleAssignments = {
"__deferred" = {
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)/RoleAssignments";
};
};
StepChangePillar = "<null>";
Title = "JIRA Governance";
"__metadata" = {
etag = "\"14\"";
id = "Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)";
type = "SP.Data.Community_x0020_DiscussionListItem";
uri = "https://xxxxxxx/_api/Web/Lists(guid'1594ea5e-0ca7-4de3-81aa-4082bde336a4')/Items(3)";
};
},
Thanks
You should have an array of dictionaries correct?
If so this should work:
NSInteger index = 0; //You can set this to the current page of your scroll view
NSArray * items = /* YOUR ARRAY OF DICTIONARIES */;
NSDictionary * dict = [items objectAtIndex:index];
NSString * title = [dict objectForKey:#"Title"];
You might also consider making a subclass of NSObject and building in a parser for your dictionary so you can access the variables with strong types.
i am developing instragramm type app when user tap like button i want add username in likes arry
So I have a dictionary in a dictionary with arrays. I am trying to add to the arrays that are in the likes array.
{
caption = "Shakira Shakira...";
comments = (
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 53;
likes = (
);
"post_type" = i;
"total_comments" = 0;
"total_likes" = 0;
"upload_dt" = 3wk;
"user_id" = 7;
"user_liked" = 0;
},
{
caption = "Addicted to Shakira???";
comments = (
{
comment = "love your profile :)";
"display_name" = Rihanna;
"user_id" = 5;
},
{
comment = "You are my favorite celebrity on Follow Me.";
"display_name" = Pitbull;
"user_id" = 4;
},
{
comment = "Looking Good!";
"display_name" = "Justin Bieber";
"user_id" = 2;
}
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 52;
likes = (
{
"display_name" = "Justin Bieber";
"user_id" = 2;
},
{
"display_name" = Rihanna;
"user_id" = 5;
},
{
"display_name" = Pitbull;
"user_id" = 4;
}
);
"post_type" = i;
"total_comments" = 3;
"total_likes" = 3;
"upload_dt" = 2mth;
"user_id" = 7;
"user_liked" = 0;
},
i want add display_name and user_id
NSMutableArray *mutearr = [NSMutableArray arrayWithArray:array];
here array is NSArray and mutarr is NSMutablearray
You can use addObjectsFromArray: from NSMutableArray class
NSArray *animal = [[NSArray alloc]initWithObjects:#"Lion",#"Tiger",#"Dog",#"Cat",#"Sheep",#"Wolf", nil];
NSArray *color = [[NSArray alloc]initWithObjects:#"Blue",#"Red",#"Yellow",#"Green",#"Black", nil];
NSMutableArray *all = [[NSMutableArray alloc] init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
var displayNames = [String]()
var userIds = [Int]()
let responseArray = (yourArray as? [NSDictionary]) ?? []
for ar in responseArray{
let display_name = ar.value(forKey: "display_name") as? String
displayNames.append(display_name ?? "")
let user_id = ar.value(forKey: "user_id") as? Int
userIds.append(user_id ?? 0)
}
print(displayNames,userIds)
I am dealing a very basic problem, I have two dictionary keys I need to compare but when I do it doesn't give me correct answer, somehow I am failing to create the logic.
So I have this in a plist array of dictionaries and json array comming from server
I want to compare them if event exist in plist do certain thing if not do another thing;
PROBLEM:
According to my logic if two events in plist are equal to other two events comming from server if logic should be printed per for each dictionary coming from server , so in this case 2 times total , but in my code it is printed 4 times. basically it is satisfying the condition every single time.
NSLOG:
evreka find the id
evreka find the id
evreka find the id
evreka find the id
SOURCE:
in a plist;
createList (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
This comming from server
responseobj (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof#gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
CODE:
Comparison code;
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
createList= [NSMutableArray arrayWithContentsOfFile:dataPath];
NSLog(#"createList %#",createList);
NSLog(#"responseobj %#",responseObj);
//compare plist and response object
for (int i=0; i<[responseObj count]; i++) {
NSDictionary *dataDict = [responseObj objectAtIndex:i];
NSString *event_name =[dataDict objectForKey:#"event_name"];
//NSString *event_id =[dataDict objectForKey:#"event_id"];
BOOL eventExistInPlist=NO;
for (int j=0; j<[createList count]; j++) {
NSDictionary *createDict = [createList objectAtIndex:i];
//NSString *create_id =[createDict objectForKey:#"event_id"];
NSNumber *create_id =[createDict objectForKey:#"event_id"];
if ([[dataDict objectForKey:#"event_id"] isEqual:[createDict objectForKey:#"event_id"]]) {
// if (create_id==event_id) {
NSLog(#"evreka find the id");
What I have tried:
I have tried to compare them as strings(isEqualtoString), numbers(==) and isEqual: method , they have all failed.
What am I doing wrong?
In
NSDictionary *createDict = [createList objectAtIndex:i];
^----- HERE
you are using the wrong index, it should be j instead of i for the inner loop.
Martin R's answer is where your issue is. I would change your logic though to include Objective-C fast enumeration instead of the standard for ++ loop. I also separated out the second for loop into a BOOL method for easier reading
for (NSDictionary *dataDict in responseObj) {
NSString *event_name =[dataDict objectForKey:#"event_name"];
if ([self matchFoundForEventId:event_name]) {
NSLog(#"Match found");
}
}
-(BOOL)matchFoundForEventId:(NSString *)eventId {
for (NSDictionary *dataDict in createList) {
NSString *createEvent = [dataDict objectForKey:#"event_id"];
if ([eventId isEqualToString:createEvent]) {
return YES;
}
}
return NO;
}
I have an NSMutableArray which contains an NSDictionary, I need to sort the array based on certain criteria.
Sorting criteria is
sort based on properties.property.value, suppose if I want to sort based on properties.property.name = Risk value. I want all the elements with Risk value first with the value in Ascending order. That means for the Dictionary which doesn't have
properties.property.name = Risk must come last. Some of the dictionaries don't have these names.
Please help...
finalArr (
{
ViewTag = 101;
"action-taken-date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "3 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 102;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "2 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 3;
}
);
};
{
ViewTag = 104;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Priority;
name = priority;
value = 1;
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 2;
}
);
};
)
Try with this
NSString *risk = #"Risk";
[finalArray sortUsingComparator:^NSComparisonResult(NSDictionary * dict1, NSDictionary * dict2) {
NSDictionary *property1 = dict1[#"properties"][#"property"][0];
NSString *name1 = property1[#"name"];
NSDictionary *property2 = dict2[#"properties"][#"property"][0];
NSString *name2 = property2[#"name"];
if ([name1 isEqualToString:name2]) {
//if both names are equal then they are sorted based on value
NSString *value1 = property1[#"value"];
NSString *value2 = property2[#"value"];
return [value1 compare:value2 options:NSNumericSearch];
}else if ([name1 isEqualToString:risk]){
//if name1 is equal to risk it is moved up
return NSOrderedAscending;
}else{
return NSOrderedDescending;
}
}];
It sounds like you have multiple different requirements (logic) which go into determining the desired sort result. You should write your own comparison logic and use one of the array methods like sortUsingComparator:.