How to extract app information from the Apple store? - json

I'm a newbie with Json, so I want to extract some app information from the app store, I found this question here :
How to get info from the Apple iTunes «App Store» and «Mac App Store»
It allows to get a Json file containing all the app information, but it would be nice if someone could show me how to get the price for example.
Thanks.

Where and how do you want to collect the results? Is this for an app or for some other kind of tool? In the reuslting JSON you have an array "results" where each element has a "currency" and a "price" value. Those can be queired rather simply. For example using the iOS 5 NSJSONSerialization (note this is an example, there has to be more error handling, handling of no / too many entries etc):
// assuming you have the downloaded data
NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:downloadedData options:nil error:&error];
if (error != nil)
{
// error handling
}
else
{
NSArray *results = [jsonData objectForKey:#"results"];
// assuming you have 1 valid entry
NSDictionary *result = [results objectAtIndex:0];
NSNumber *price = [result objectForKey:#"price"];
NSString *currency = [result objectForKey:#"currency"];
NSLog(#"Price is %# #%", price, currency);
}
Similar ways to access the data are avilable in other JSON frameworks, just grad the results array and every dictionary therein has a price and currency value.

Related

NSJSONSerialization not handling negative integers

I’m trying to parse an JSON file into an NSArray and it all works well for positive numbers. However all the negative integers in that JSON file produce high numbers like “[11] __NSCFNumber * (long)72057594037927933”. How can i get that to work?
Here is my JSON file:
[0,1,2,3,4,5,6,7,8,9,10,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11]
and here the code:
NSError* error;
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://some/json/file.json"]];
NSMutableArray* array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
Xcode doesn't always print nice values into the Debug area of the Xcode (where live values for the context appear).
I dropped your code into my own project and when I step through each line, I do see the "eachNumber __NSCFNumber * (long)72057594037927934 {0xbfffffffffffffe3} bits, but if I try print out the values of the array into the console, you'll see the correct "-2" result.
Try it yourself. I added in these lines right after yours:
NSMutableArray* array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(#"array is %#", array);
for(id eachNumber in array)
{
NSLog(#"eachNumber is %#", eachNumber);
}

Trouble loading data for TableView from server

Im using a web-service API to load information from a DB (mySQL). When transitioning to a UITableViewController, I retrieve an array that contains categories in the VieDidLoad method. However, I am not able to display the array of categories that I got into the table view cells. When I init the category NSMutubaleArray and hardcode it on the otherhand, the tableview does display the information. I am trying to understand what I'm doing wrong in the steps of loading this array and populating it onto the tableview. Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//When I init the _categoryList the way below, it displays the info.
//_categoryList = [[NSMutableArray alloc] initWithObjects:#"Trending",#"Sports",#"Portraits", #"Art",#"IdUser", nil];
_toCategory = [[NSString alloc]init];
//When I try to populate it from the server it doesnt work.
_categoryList = [[NSMutableArray alloc]init];
//just call the "getCategories" command from the web API
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"getCategories",#"command",
nil]
onCompletion:^(NSDictionary *json) {
//got stream
NSArray *tempArray = [json objectForKey:#"result"];
for (int i=0;i<tempArray.count;i++){
[_categoryList insertObject:[tempArray objectAtIndex:i] atIndex:i];
}
//I verify that I was able to retrieve the array with the correct conent.
[self printArray:_categoryList];
NSLog(#"got categories");
[self.tableView reloadData];
}];
}
Any help would be greatly appreciated. Thanks!
UPDATE #1:
The JSON return is an array with NSDictioanry's. I print out its contents just to make sure, by calling the JsonArray objectForKey[‘categories’] and get this:
SwiphtMVPClone[2410:11303] Array at Index 0 = Trending
SwiphtMVPClone[2410:11303] Array at Index 1 = Sports
SwiphtMVPClone[2410:11303] Array at Index 2 = Portraits
SwiphtMVPClone[2410:11303] Array at Index 3 = Art
Right now, my app is actually crashing – getting this error:
[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x72a85d0

Fast Enumeration in parse

I'm new to iOS and I'm trying to parse out JSON data. Nothing will log from inside of the enumeration, though vehicleActivity logs "vehicle (null).I am interested in logging the "LineRef" data. I'm assuming it is in NSString, but I tried with id object and still nothing.
NSDictionary *jsonParse = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSArray * vehicleActivity = [jsonParse objectForKey:#"VechicleActivity"];
NSLog(#"vehicle %#",vehicleActivity);
for (NSDictionary *dictionary in vehicleActivity ) {
NSDictionary *monitoredVehJourney = [dictionary objectForKey:#"MonitoredVehicleJourney"];
NSString *line = [monitoredVehJourney objectForKey:#"LineRef"];
NSLog(#"Line # %#",line);
Here is the JSON through a viewer
Thanks
though vehicleActivity logs "vehicle (null)"
That's why. If vehicleActivity is NULL (i. e., nil), then the fast enumeration wouldn't even begin.
Furthermore, you seem not to have read the documentation of the NSDictionary class. It iterates through keys, not values. So your for loop should be
for (NSString *key in vehicleActivity ) {
NSDictionary *dictionary = [vehicleActivity objectForKey:key];
// etc.
}
instead.
Furthermore, I believe you have a typo in your code regarding the key VehicleActivity. You wrote it as VechicleActivity.

iOS Parsing JSON Woes

I've been bashing my head against a wall for a bit.
I have a rails back-end that is returning JSON to my iOS app. I'm using rails' default return for rendering my object in JSON for me automatically. I'm having trouble with the errors it returns.
The JSON I get for errors is {"errors":{"email":["can't be blank"],"password":["can't be blank"]}}.
I use ASI for handling the request.
-(void) requestFinished:(ASIFormDataRequest *)request {
NSDictionary *data = [[request responseString] JSONValue];
Doing the above code make data become:
{
errors =
{
email = (
"can't be blank"
);
password = (
"can't be blank"
);
};
}
Now this gives me issues trying to parse it out. I'd like to be able to access each element of errors, and its associated value.
When I try to loop through the elements, I do something like:
for (NSDictionary *error in [data objectForKey:#"errors"])
This will give me email and password, but they are of type __NSCFString, not NSDictionary. I haven't been able to find a way to get the value for either email or password. Does anyone have an idea on how to parse this out?
Thanks!
This should work, note that response has the same structure than your 'data' NSDictionary.
NSDictionary *fields = [[NSDictionary alloc] initWithObjectsAndKeys: [[NSArray alloc] initWithObjects:#"one", #"two", nil],
#"A",
[[NSArray alloc] initWithObjects:#"three", #"four", nil],
#"B",
nil];
NSDictionary *response = [[NSDictionary alloc] initWithObjectsAndKeys:fields, #"errors", nil];
NSLog(#"Dictionary: %#", [response objectForKey:#"errors"]);
for (NSString *field in [response objectForKey:#"errors"])
for (NSString* error in [response valueForKeyPath:[NSString stringWithFormat:#"errors.%#", field]])
NSLog(#"%# %#", field, error);
The output will look like this:
Dictionary: {
A = (
one,
two
);
B = (
three,
four
);
}
A one
A two
B three
B four
Well i dont have Mac right now but i'm trying to help you if it doesnt work let me know i will correct it tomorrow.
-(void) requestFinished:(ASIFormDataRequest *)request
{
NSArray *data = [[request responseString] JSONValue];
NSDictionary *dict = [data objectAtIndex:0];
NSDictionary *dict2 = [dict valueForKey:#"errors"];
NSLog(#"email = %#, password = %#",[dict2 valueForKey:#"email"], [dict2 valueForKey:#"password"]);
}

How do you parse a JSON array with TouchJSON?

I'm using TouchJSON. Made a call to my Rails app to fetch all "posts" at localhost:3000/posts.json. This returns a JSON array, surrounded by square brackets. I'm currently set up to convert the jsonString into an NSDictionary, but that fails due to the square brackets.
NSString *jsonString=[self jsonFromURLString:#"http://localhost:3000/posts.json"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
self.dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
What's the best way to turn this result into an NSArray using the TouchJSON library?
Error Domain=kJSONScannerErrorDomain Code=-101 "Could not scan dictionary. Dictionary that does not start with '{' character."
Why don't you just use the -deserialize: method and then figure out what kind of object you have after its done?
Or to answer your specific question, use -deserializeAsArray:
I think most parsers frown with array data. You'll have to return a hash:
{'result': [Your array data]}