I could able to POST the parameters as follows, it works if I have only one item for each dictionary item. In the following params I have only one pName and one price.
NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
#{#"pName":pData.pName,
#"price":pData.price,
#"notes":pData.notes}];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"text/html",nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:URL_SIGNIN parameters:params progress: nil
success:^(NSURLSessionTask *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:
^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
However, I wonder how could I able to put items as a parameters if I have more than one items such as pNames = [Beef, Coffee, Rice ,Sprite] and prices = ["$10", $"3", "$5", #"1"].
Consider as following object at the end.
orders = {#"Beef" : #"$10",#"Coffee" : #"$3", #"Rice" : #"$5", #"Sprite" : #"$1"}
Assume that it is restaurant application where user selects multiple items to check out.
You want an array ($[]), not a dictionary (${}).
In your example:
prices = #[#"$10", #"$3", #"$5", #"$1"];
EDIT:
As parameters:
NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
#{#"prices": #[#"$10", #"$3", #"$5", #"$1"]}];
OR:
NSArray *prices = #[#"$10", #"$3", #"$5", #"$1"];
NSMutableDictionary *params= [NSMutableDictionary dictionaryWithDictionary:
#{#"prices": prices];
Related
I'm trying to check if NSString 'testing' (47) exists inside of my NSMutableArray 'self.checkfriendData'. I'm using the code below, though after logging my if statement it appears as though it's never executed (even though the statement is true - see console data below, uid = 47, and thus hiding my object should fire?) Any idea as to why this isn't working? Help is much appreciated!
ViewController.m
NSMutableDictionary *viewParams3 = [NSMutableDictionary new];
[viewParams3 setValue:#"accepted_friends" forKey:#"view_name"];
[DIOSView viewGet:viewParams3 success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.checkfriendData = (NSMutableArray *)responseObject;
NSString *testing = #"47";
NSArray *friendorNo = self.checkfriendData;
if ([friendorNo containsObject:testing]) // YES
{
self.addFriend.hidden = YES;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
Here's what's inside self.checkfriendData:
2017-05-18 19:36:07.266529-0700 This is the friend data check (
{
body = "My name is Britt";
friendphoto = "/sites/default/files/stored/x.jpg";
"node_title" = "Britt";
uid = 47;
}
)
It appears that your NSArray contains NSDictionarys and you are asking if the array contains an NSString. The answer will always be no as the array doesn't directly contain any NSStrings.
If you want to search for the uid of 47 you will have to iterate over the array and check the uid key of each NSDictionary for the value 47.
The code for this would look something like:
for (NSDictionary *dict in friendorNo) {
if ([dict[#"uid"] isEqualToString:testing]) {
self.addFriend.hidden = YES;
}
}
I've retrieved data to my iOS app from my Drupal database using the below code:
.h
#property (strong, nonatomic) NSMutableArray *userData;
.m
self.userData = [NSMutableArray new];
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:#"u000" forKey:#"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.userData = [responseObject mutableCopy];
NSLog(#"%#",self.userData);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", [error localizedDescription]);
}];
self.userData = [[NSMutableArray alloc] init];
self.userBio.text = self.userData[0][#"userbio"];
self.userData is populated, so I know my data has successfully been returned. Does anyone know how I would display the JSON returned field in a UIlabel? I'm just not sure how I would write this line of code (newb, sorry). My field name is userbio.
Here's how the data structure is returned (incase it's of any use):
UPDATE: NSLog(#"userData = %#", self.userData); returns this in the console:
2015-10-25 11:22:06.601 [7605:2738034]
userData = (
> {
> userbio = "No user bio available at this time.";
> "users_name" = "<a href=\"/user/20\" title=\"View user profile.\" class=\"username\" xml:lang=\"\" about=\"/user/20\"
> typeof=\"sioc:UserAccount\" property=\"foaf:name\"
> datatype=\"\">Brittany</a>";
> },
If it's an NSDictionary the code would look like this:
self.myLabelName.text = self.userData[#"userbio"];
If it's an NSArray of NSDictionaries it would look like:
self.myLabelName.text = [self.userData firstObject][#"userbio"];
That's assuming that the array contains only one dictionary, otherwise you would have to access each element: self.userData[0], self.userData[1], etc.
Im newly using AFNetworking to get JSON and parse it,I've imported it to my project and got the JSON but i don't know how is it possible to parse the JSON for display especially in Objective-c.
Here is the code in my viewDidLoad to Get JSON :
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://api.androidhive.info/json/movies.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
The Retrieved JSON for parsing:
{
genre = (
Action,
Drama,
"Sci-Fi"
);
image = "http://api.androidhive.info/json/movies/1.jpg";
rating = "8.300000000000001";
releaseYear = 2014;
title = "Dawn of the Planet of the Apes";
},
{
genre = (
Action,
"Sci-Fi",
Thriller
);
image = "http://api.androidhive.info/json/movies/4.jpg";
rating = "8.4";
releaseYear = 2014;
title = "X-Men: Days of Future Past";
},
{
genre = (
Action,
Adventure,
Fantasy
);
image = "http://api.androidhive.info/json/movies/7.jpg";
rating = "7.3";
releaseYear = 2014;
title = "The Amazing Spider-Man 2";
},
{
genre = (
Animation,
Comedy,
Family
);
image = "http://api.androidhive.info/json/movies/9.jpg";
rating = "8.300000000000001";
releaseYear = 2013;
title = Rush;
},
{
genre = (
Animation,
Adventure,
Family
);
image = "http://api.androidhive.info/json/movies/15.jpg";
rating = "8.199999999999999";
releaseYear = 2010;
title = "How to Train Your Dragon";
}
)
Update :
How is it possible to display it and store the retrieved data so its easier to show it in UITableView.
i tried to do like :
NSLog(#"JSON: %#", [responseObject objectForKey:#"image"]);
to get the whole images only but it crashes. i just need to get for example all the titles and store them in array and then display them UITableView.
you try this way to get data from NSDictionary
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://api.androidhive.info/json/movies.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
self.arrData=[[NSMutableArray alloc]initWithArray:responseObject];
[self.tableView reloadData];// reload table data
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
TableView Delegate Method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// your cell code here
// indexPath.row(use in tableView) means your number of index to get value
NSLog(#"JSON: %#", [[self.arrData objectAtIndex:indexPath.row] objectForKey:#"image"]);
NSLog(#"title: %#", [[self.arrData objectAtIndex:indexPath.row] objectForKey:#"title"]);
NSLog(#"rating: %#", [[self.arrData objectAtIndex:indexPath.row] objectForKey:#"rating"]);
NSLog(#"releaseYear: %#", [[self.arrData objectAtIndex:indexPath.row] objectForKey:#"releaseYear"]);
NSLog(#"genre: %#", [[self.arrData objectAtIndex:indexPath.row] objectForKey:#"genre"]);// return array get value particular using array index
}
AFNetworking returns already processed JSON in responseObject - and in your case it's an NSArray of NSDictionary objects inside it.
To access a single NSDictionary create an NSArray property (e.g. resultArray) and set it to responseObject, then you can access its objects by resultArray[1] (make sure to bounce check).
Then you can access the values of the dictionary by their keys (myDictionary[#"title] and so on).
I'm using AFNetworking to add Book objects to a server. Here's the setup:
Some dummy Book objects are created for testing:
Book *newBook1 = [[Book alloc] init];
newBook1.title = #"Test Book 1";
newBook1.author = #"Harlan";
Book *newBook2 = [[Book alloc] init];
newBook2.title = #"Test Book 2";
newBook2.author = #"Harlan";
NSArray *books = [NSArray arrayWithObjects:newBook1, newBook2, nil];
[bookCommunicator addBooks:books];
Inside BookCommunicator:
- (void)addBooks:(NSArray *)books
{
for(Book *book in books)
{
[self addBook:book];
}
}
- (void)addBook:(Book *)book
{
NSDictionary *bookProperties = [NSDictionary dictionaryWithObjectsAndKeys:
book.author, #"author",
book.title, #"title",
nil];
[_httpSessionManager POST:#"books" parameters:bookProperties
success:^(NSURLSessionDataTask *task, id responseObject) {
[self.delegate didAddBooks];
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
[self.delegate addingBooksFailedWithError:error];
}
];
}
I get a success response. The responseObject is an array of all of the Book objects already on the server - excluding the new ones. Upon checking the server, these two new book objects are not there. Any ideas as to why?
The issue was not with the code above, but with the API. It required a trailing / on the endpoint (i.e. 'books/') - but it should have accepted both 'books' or 'books/'.
I come from Android dev, so sorry if I'm missing obvious iOS concepts here.
I have a JSON feed that looks like:
{"directory":[{"id":0,"fName":"...","lName":"...","title":"...","dept":"...","bld":"...","room":"...","email":"...","phone":"..."},{"id":1,"fName":"...","lName":"...","title":"...","dept":"...","bld":"...","room":"...","email":"...","phone":"..."}]}
Then, I have a Staff.h and .m with a class with properties to match it (id, fName, lName) ect.
I've been working at this for hours, but I can't seem to parse the JSON string to an array of Staff objects. The end goal is to get them into Core Data, so any advice would be nice.
Tutorials I've read haven't shown how to work with a JSON string in the form of {"directory":[{...}]} I had no problem doing this in my Android app, but I'm out of ideas here for iOS (6) in objective-c.
Thanks for reading.
You can do it like
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];//response object is your response from server as NSData
if ([json isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment.
NSArray *yourStaffDictionaryArray = json[#"directory"];
if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){//Added instrospection as suggested in comment.
for (NSDictionary *dictionary in yourStaffDictionaryArray) {
Staff *staff = [[Staff alloc] init];
staff.id = [[dictionary objectForKey:#"id"] integerValue];
staff.fname = [dictionary objectForKey:#"fName"];
staff.lname = [dictionary objectForKey:#"lName"];
//Do this for all property
[yourArray addObject:staff];
}
}
}
Use: NSJSONSerialization
You use the NSJSONSerialization class to convert JSON to Foundation
objects and convert Foundation objects to JSON.
An object that may be converted to JSON must have the following properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
You will get NSDictionary then you can parse (create) it to your object and then use it in CoreData.
Use following code:
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
This will covert json data onto NSDictionary, which is similar to hashmap on android. I think this will help you. :)
you can use NSJSonSerialisation or AFNetworking library. Here is the example of AFNetworking to parse json response
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSDictionary *json = (NSDictionary *)responseObject;
NSArray *staffArray = json[#"directory"];
[staffArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
Staff *staff = [[Staff alloc] init];
staff.id = [[obj objectForKey:#"id"] integerValue];
staff.fname = [obj objectForKey:#"fName"];
staff.lname = [obj objectForKey:#"lName"];
//add data to new array to store details
[detailsArray addObect:staff);
} ];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
then use Core Data framework to store data.
I would take a look at RestKit. It provides object-mapping and CoreData backed storage.
For this, you can SBJSON framework.
You have to convert the response string into an NSDictionary like
NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *dic = [responseString JSONValue];
Now you can create an object for Staff class.
Staff *staff = [[Staff alloc]init];
Then you can store values in this object like
staff.firstname = [[[dic objectForKey:#"directory"] objectAtIndex:0] objectForKey:#"fName"];
Now you can pass this single object to other classes
You can use the handmade solution proposed by #janak-nirmal, or use a library like jastor, https://github.com/elado/jastor, it doesn't make much difference.
I warn you against Restkit, because the ratio benefits-vs-pain is very low, in my opinion.
Moreover, it could be as use a tank to kill a fly in your scenario.