0

I'm learning a very basic method to download data from a weather api. Basically trying to follow a tutorial.

Using the URL, I am able to download the data in JSON format into a dictionary. Then put into an array.

My question now is how do I read the particular value of an item in the array.

For example, when I do an NSLOG of the array I get the following... I only cut/paste a couple as there are 55 items.

So my question is how do I grab a particular value our of this array?

2013-03-18 14:37:57.576 LocalWeatherV3[1220:c07] loans: {
    UV = 2;
    "dewpoint_c" = "-4";
    "dewpoint_f" = 24;
    "dewpoint_string" = "24 F (-4 C)";
    "display_location" =     {
        city = "Jersey City";
        country = US;
        "country_iso3166" = US;
        elevation = "47.00000000";
        full = "Jersey City, NJ";
        latitude = "40.75180435";
        longitude = "-74.05393982";
        state = NJ;
        "state_name" = "New Jersey";

        zip = 07097;
    };
    estimated =     {
    };
    "feelslike_c" = 2;
    "feelslike_f" = 35;
    "feelslike_string" = "35 F (2 C)";
    "forecast_url" = "http://www.wunderground.com/US/NJ/

here is a piece of the .m

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1

                          options:kNilOptions
                          error:&error];

    NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2

    NSLog(@"loans: %@", latestLoans); //3

// 1) Get the latest loan
//NSDictionary* loan = [latestLoans objectAtIndex:1];
NSInteger counter = [latestLoans count];

thanks in advance!!

so when I do this

NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];

and i mouse over the local watch, I see

json    NSDictionary *  0x08d62d40
[0] key/value pair  
key id  0x08d61cf0
value   id  0x08d62100
[1] key/value pair  
key id  0x08d62150
value   id  0x08d633a0

then i do

NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2

NSLog(@"loans: %@", latestLoans); //3

and one of the items I want is in "latestloans" which is where all that data shows up. so I cant figure out how to grab one of the items

5
  • 1
    You're misunderstanding already if you say "I am able to download the data in JSON format into a dictionary. Then put into an array." The value you logged is not an array but a dictionary entry which contains within it another dictionary. There aren't any arrays in your sample -- arrays would be bracketed with []. Commented Mar 18, 2013 at 19:02
  • 1
    Study the JSON syntax here. When you NSLog Objective-C objects that have been converted from JSON the syntax is very much similar, except that [] has been replaced with (), ":" has been replaced with "=", and not all strings are quoted. The best way to feel your way along is to NSLog every step in the process. Commented Mar 18, 2013 at 19:12
  • 1
    First off, get rid of "latestLoans" (I guess you copied that from an example) and use a meaningful name. Then understand that what you have there is not (unlike the example) an NSArray but rather an NSDictionary. Change the type of the variable to NSDictionary. Commented Mar 18, 2013 at 19:14
  • thanks... I'll work on that and come back Commented Mar 18, 2013 at 19:16
  • thanks for the help... changing it to dictionary like you said made it work. also changed the variable names just to make it more readable. appreciate the input and will read up on json Commented Mar 18, 2013 at 19:47

1 Answer 1

1

Let's assume you're trying to grab the forecast url. It's as simple as:

// update this line
NSDictionary *latestLoans = [json objectForKey:@"current_observation"];
// url variable will contain the first forecast url in the array
NSString *url = [latestLoans objectForKey:@"forecast_url"];
Sign up to request clarification or add additional context in comments.

9 Comments

Except that he doesn't seem to have an array.
I believe he does have an array. He notes: "I only cut/paste a couple as there are 55 items". I assume he's only showing us one object from the array.
Yes, but he's showing the output of his NSLog that begins "loans:". That object is clearly not an array
soon as I do objectAtIndex:0 I get an error _NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8e69070 2013-03-18 15:06:14.381 LocalWeatherV3[1330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8e69070' *** First throw call stack: (0x19ae012 0x135be7e 0x1a394bd 0x199dbbc 0x199d94e 0x31d2 0x136f6b0 0xd9b765 0x1931f3f 0x193196f 0x1954734 0x1953f44 0x1953e1b 0x28187e3 0x2818668 0x29fffc 0x200d 0x1f35) libc++abi.dylib: terminate called throwing an exception
@solarissf - Like I said, you don't have an array. (Please read a hundred other threads of the same ilk and find out how to "read" JSON.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.