Need some help accessing JSON Data iPhone - iphone

Disclaimer: I'm new to JSON.
I have a website that offers up it's Data in JSON format and I'm in the process of building an iPhone App to access that data. The problem I have is not actually with the app itself but rather the syntax to access some of the JSON data.
I don't have any issue accessing the data in the Post [ ] Section, however I want to access Attachments > Images > wptouch-new-thumbnail > url
I know you usually would access the data with a dot syntax but with everything I have tried I have been unable to select anything outside of the Post[] section.
it's pretty simple I take what I get from the server through posts and add them to NSStrings or UIImage in the case of the images for use in a UITableView.
I have all of that working however I can't seem to access anything under attachments. I tried to make a different NSDictionary for attachments but that did not work.
Again remember I'm very new to JSON / Accessing data within it.
Any help would be appreciated!

Make NSMutableData as jsonMutableData instead of NSMutableString.
in connection dedreceiveresponse, initialize jsonMutableData then,
//Json Connection
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[jsonMutableData appendData:partialData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonData = [[NSString alloc] initWithData:jsonMutableData encoding:NSUTF8StringEncoding];
NSDictionary *filesJSON = [jsonData JSONValue];
NSArray *files = [filesJSON objectForKey:#"posts"];
//further code

Related

Ordering multiple Asynchronous JSON parses from website

Quick brief of what I'm doing: I have two arrays, they both contain 50% of the information for a table view. Why? Because one of the arrays pulls current information from the internet, while the other array has saved user data. I had no idea as to how to get current information from the internet in a non messy way, as I'm an amateur to objective-C let alone networking in Objective-C. Anyway, so the internet Array is pulling information that corresponds to the objects in the saved array (saved in Core Data) using AFNetworking. It's therefore Asynchronous, which I want. Now here comes the problem, or at least what I can garner from the situation.
I'm doing a for loop that effectively counts through the objects in the saved array and passes the unique ID for each object so that the corresponding information from the internet can be downloaded, parsed and added to the internet array. However, since the networking is Asynchronous, the loop is effectively downloading all the information from the internet at once. Therefore the objects are being written to the internet array in order of which downloaded first. Therefore savedArray[0] does not correspond to the object in internetArray[0]. This is a pretty big flaw as you can imagine, as when I'm inserting the values into my tableView, nothing really matches up/makes sense.
I'm really looking for a way to postpone the downloading of the information, until the previous download has been completed and added to the internetArray, how on earth do I do this? Now for the code. Here is where I get the appropriate key:
for ( int i = 0; i < [mySavedObjects count]; i++) {
MySavedObject* mySavedObject = [mySavedObjects objectAtIndex:i];
[self retrieveMissingInformation: myObject.Id];
[self.tableView reloadData];
}
And here is where I actually get the information (simplified for the sake of space):
- (void)retrieveMissingInformation:(NSString *) Id
{
// create URL here.
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Success");
// Do some safety checks & parse the JSON, returning a searchResult.
[searchResults addObject:searchResult];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
// Do some error messages etc.
}];
[queue addOperation:operation]; //where queue is an ivar NSOperationQueue.
}
Finally, in the cellForRowAtIndexpath, I these use both:
MySavedObject *mySavedObject = [mySavedObjects objectAtIndex:indexPath.row];
&
SearchResult *searchResult = [searchResults objectAtIndex:indexPath.row];
To get the values for the cell.
Sorry for the massively large wall of text. I'm not really good enough to explain things efficiently, often getting tongue-tied on the terminology and having to resort to making up my own exmaples. Any help with ordering this mess and I'd be really grateful.
Regards,
Mike.
Good work mate, you've stumble on a common design pattern.
As you've seen, with asynchronous request, execution happens almost concurrently so you're not guaranteed order when you think of "queues" in terms of a for loop.
The way you need to think of your download queue is in terms of recursion.
In other words, instead of:
// your current method of queue-ing the submission of data
for(int i = 0; i < arrData.count; i++)
{
[self doAsyncDataSendForIndex:i];
}
You need to start doing something like this:
// perform the first submit for the first element in your array
[self doAsyncDataSendForIndex:dataIndex];
// a completion callback method
-(void)asyncFinishDownloading
{
[self parseResult:serverReturnedData];
}
-(void)parseResult:(id)serverData
{
// do all the processing of the data that you need here
...
// update index for next round
dataIndex++;
// -----------------------------------------------------------------
// now at the end of this method, perform a recursive call
// of your async download method again
// -----------------------------------------------------------------
[self doAsyncDataSendForIndex:dataIndex];
}
Using the callback delegate way of queueing your download you are telling the code to only download the next data after it has finished processing the last asynchronous download.
Now you can use a number of libraries to help you with your asynchronous stuff. I myself use ASIHttpRequest and ASIFormData, you can use AFNetworking too which is newer.
-(void)asyncSubmitData:(id)someData
{
NSURL *url = [NSURL urlWithString:#"http://www.yourserver.com/...."];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// setup your POST request parameters
[request setPostValue:someData forKey:#"someKey"];
...
// setup the completion callback delegate action
[request setCompletionBlock:^{
// process your server returned data here
NSString *serverResponse = [request responseString]; // raw server response
...
// increment your index and call your download method again
dataIndex++;
[self asyncSubmitData:[myArray objectAtIndex:dataIndex]];
}];
// setup fail block in case the server fails
[request setFailedBlock:^{
NSLog(#"Server error: %#", [[request error] localizedDescription];
}];
// start the asynchronous request now
[request startAsynchronous];
}
Hope that helps.
I think I understand your problem and it sounds like you're using the wrong data structures to store the data.
You have one array that is generating requests and the results to those requests are stored in a separate, unrelated array.
As you generate the request for information with an objectId, can you store the results in a NSDictionary instead of an array and use the objectId as the key to this array?
You have the NSURLRequest in the success method so you should be able to retrieve your objectId from the request through the NSURL.query if it is a parameter on the query string
Postponing the downloading of info and effectively making all of your network requests serial (one at at time) would work, but isn't a very nice solution, because it's often handy to have concurrent network requests going on - for example, allowing four requests at once would probably take less time than making all requests serially.
A much better solution is to correctly handle a piece of data when it comes back. To do this you need to handle the returned data in a more structured way. Rather than just appending it to an array, do one of the following:
A. Put returned data in a dictionary. The dictionary can map from an index or appropriate key to the returned data. (Hint: if you want to put an int as a key in a dictionary, construct an NSNumber containing that int.) This option is recommended over B.
B. Insert the returned data into an array. In order for ths to make sense, before you do any fetching, you should fill up your array with placeholder objects that denote "this bit of data hasn't been fetched yet". One possible placeholder object choice would be NSNull - it's just an object to denote null/nil.
So then you can return the correct item in cellForIndexPath: by dipping into your dictionary or array.
These two data structure ideas are just the most obvious ones that come to mind, there are doubtless other ways to crack this nut.

RestKit - Appending a parameter to all requests

I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request sent using the RKObjectManager's methods.
I've tried creating a Category overload of NSManagedObject in order to use the following method:
- (void)willSendWithObjectLoader:(RKObjectLoader *)objectLoader
That works fine, however I see no way of appending to the object loader's params. I've even gone as far as to reserialize the object but there is no way to do that without it being sent using escape characters. For example, the following code will give me the object in JSON serialized form
RKObjectMapping *serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[self class]];
RKObjectSerializer *ser = [RKObjectSerializer serializerWithObject:self mapping:serializationMapping];
NSObject<RKRequestSerializable> *obj = [ser serializationForMIMEType:RKMIMETypeJSON error:nil];
But the object returned there is intend to be used as a param right away so the following code does not work
[params setValue:[LCSingletonLoggedInUser sharedLoggedInUser].sessionToken forParam:#"token"];
[params setData:obj.HTTPBody forParam:#"data"];
I've also tried various combinations of setObject and setData and obj.HTTPBody as well as just obj right away.
Actually appending obj to params in any way will always result in it adding escape characters which the server can't handle. Setting params = obj will give the correct values to the server but won't include the token.
How about adding it to queryParams?
NSString *resourcePath = [#"/products" stringByAppendingQueryParameters:_queryParams];
Where queryParams is a NSMutableDictionary where you add your token.

Can more than one Web Services be called at the same time from iPhone?

I have made a social networking application and I m calling various Web Servies to get the Users's different data like the Friend list, the latest updates etc.
But while I m calling one Web Service in background, and then make call to another, the first one stops, no response comes back for the first one...
What should be done for that?
Is it not possible to call two Web Services at a time??
I am not sure how you are calling services. If you will show your code, any one can help you more...
you can try this -
- (NSData *)fetchProfileData:(NSString *)accessToken{
NSURL *url = [NSURL URLWithString:serverURL];
request= [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSError *error;
NSHTTPURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
return responseData;
}
to call other web services create separate separate functions for each service like
- (NSData *)fetchFriendsList:(NSString *)accessToken{
//write your logic, you see above method for more details
}
- (NSData *)fetchUpdates:(NSString *)accessToken{
//write your logic, you see above method for more details
}
call above methods from your view controllers like
[NSThread detachNewThreadSelector:#selector(callFetchProfileDataServices:) toTarget:self withObject:#"userToken"];
-(void)callFetchProfileDataServices:(NSString*)token{
NSData *response = [self fetchProfileData:token];
//now parse response data data using suitable parser
}
You need to create separate separate threads for each services and you may save these data in member field also .

how to do json parsing in iphone

i am using json parsing in my application
and this is my json data is as follow :
{"response" :
{"success":false,"error":
{"code":7,"description":"You are not logged in"}}}
and i want description means "You are not logged in" i my string
so how can i do that
please help me.....
Check the blog post with sample code and step by step parsing of JSON.
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://pessoal.org/blog/2008/12/12/iphone-sdk-parsing-json-data/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
We're using CJSONDeserializer (TouchJSON library) in the iPhone app being developed at work.
Just use the following method:
NSDictionary * dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
where data is of type NSData *. It will convert the JSON string into a dictionary so you could get the value of description as follows:
[[[dictionary objectForKey:#"response"] objectForKey:#"error"] objectForKey:#"description"];

How to Separate HTML Response

I am using FConnect in my app. After the user logs in to his/her Facebook account I am getting his/her information as:
{
"id":"###########",
"name":"Vishnu Gupta",
"first_name":"Vishnu",
"last_name":"Gupta",
"link":"facebook a/c link id=#######",
"education":[{"school":{"id":"######","name":"st.joseph"},"type":"High School"}],
"gender":"male","email":"##########",
"timezone":5.5,
"locale":"en_US",
"verified":true,
"updated_time":"2010-11-27T10:10:25+0000"
}
I want to store user's information in database. To do that, I have to separate the user's information that is id, name, and email.
I tried using componentsSeparatedByString method but I do not understand how to retrieve the information in an array.
Can anyone help me????
The response you are getting is in JSON Format which basically have response in Array & Dictionary.
You will be getting this data in Dictionary..so you can access it by using the key value..
NSLog(#"%#",[NSDictionaryObject valueforkey:#"id"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"name"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"first_name"]);
Hope this will surely work for you.....
ya.....its corrrect.In detail:
Add JSON SDK then write the below code
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *returnObject = [json objectWithString:InfoName];
NSString #id=[NSString stringWithFormat:#"%#",[returnObject objectForKey:#"id"]];
same for name ,email.
then Add these objects to Array and release the json
JSOn is good to use for "key-value' pair parsing.