sefirosu
sefirosu

Reputation: 2648

iOS- Cannot get expected json results back

I am trying to use google currency rest api to get USD->GBP convert result. something like this-> http://www.google.com/ig/calculator?hl=en&q=100USD=?GBP

the json is response is :
{lhs: "100 U.S. dollars",rhs: "62.9802242 British pounds",error: "",icc: true}

so i m trying to get the values from "lhs" "rhs" stored into 2 strings

here is my method for fetching converted result data :

@implementation ConvertorBrain

-(id)initWithFromCurrency:(NSString *)fromCurrency
               toCurrency:(NSString *)toCurrency
               withAmount:(int)amount
{
    if (self=[super init]) {
        self.fromCurrency = fromCurrency;
        self.toCurrency = toCurrency;
        self.amount = amount;
    }
    return self;
}

-(void)getConvertResult
{
    NSString *encodeFromCurrencyTerm = [self.fromCurrency stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *encodeToCurrencyTerm = [self.toCurrency stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *searchLocation = [NSString stringWithFormat:@"http://www.google.com/ig/calculator?hl=en&q=%d%@=?%@", self.amount, encodeFromCurrencyTerm, encodeToCurrencyTerm];
    NSURL *convertedResults = [NSURL URLWithString:searchLocation];

    NSData *JSONData = [[NSData alloc] initWithContentsOfURL:convertedResults];
    if ([JSONData length] > 0) {
        NSError *error = nil;
        NSDictionary *dictoionary = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:&error];
        if (!dictoionary) {
            NSLog(@"Json parsing failed: %@", error);
        }

        self.lhs = [dictoionary valueForKey:@"lhs"];
        self.rhs = [dictoionary valueForKey:@"rhs"];

    } else {
        [[[UIAlertView alloc] initWithTitle:@"Service Error" message:@"Cannot complete your request at this time, please try later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
}

then i called method in some other place like this :

self.brain = [[ConvertorBrain alloc] initWithFromCurrency:@"USD" toCurrency:@"GBP" withAmount:100];
[self.brain getConvertResult];

then my expected lhs and rhs results should be lhs= "100 U.S. dollars" rhs ="62.9802242 British pounds"

however, if i run the code, it throw me back these errors: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No string key for value in object around character 1.) UserInfo=0x7172860 {NSDebugDescription=No string key for value in object around character 1.}

i cannot figure out what has went wrong..need help, also, i tried to debug it a bit. it seems is this line went wrong, dosent get any data somehow -> NSData *JSONData = [[NSData alloc] initWithContentsOfURL:convertedResults];

Upvotes: 0

Views: 882

Answers (2)

Trianna Brannon
Trianna Brannon

Reputation: 1254

Your json could be incorrectly formatted. Use this to check that your json is formatted correctly. http://jsonlint.com/

Upvotes: 0

James Richard
James Richard

Reputation: 1535

Your URL looks malformed. For example: http://www.google.com/ig/calculator?hl=en&q=50USD=?GBP

Within a browser that will work fine because the browser handles special characters automatically, but that ? at the end is invalid.

Try using this URL. Be sure to escape the % in the string by putting another % after it. http://www.google.com/ig/calculator?hl=en&q=50USD=%3FGBP

Here's some other character encodings: http://www.w3schools.com/tags/ref_urlencode.asp

Upvotes: 1

Related Questions