Not receiving any results

When trying to loop through results from a FlightInfo “result set” for lack of a better way to explain it, I get no results. The returned JSON checks out valid, but no results are returned. When I try the same code on a similar Twitter result set, the values are returned. it appears I can’t just inspect “flights” but instead have to look at “FlightInfoResult” or the compiler bombs. But when I use FlightInfoResult I can’t seem to inspect any deeper into the dictionary. Am I missing something?

All FlightXML JSON responses are indeed returned with an outermost singleton element that is named “MethodResult” on success (where “Method” is the name of the method that was invoked). If an exception occurs in the JSON method, then the outermost singleton element will instead be named “error” and its value will be a textual representation of the error message.

Can you provide some specifics on what language you are using to access FlightXML JSON? Have you tried printing out the raw JSON that is returned from the server and visually comparing the structure of the output with how your code is attempting to parse it? Are you able to include a portion of the code you are using?

Attempting to use the new JSON parsing tools in iOS5, so it’s Objective-C in Xcode. The raw JSON looks like this in both the web browser and when I send it to the console using this method:



NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError];
NSLog(@"raw: %@", json);

Returns:

raw: {
    "FlightInfoResult": {
        "next_offset": 3,
        "flights": 
            {
                "ident": "FIV576",
                "aircrafttype": "C56X",
                "filed_ete": "00:55:00",
                "filed_time": 1321553537,
                "filed_departuretime": 1321555500,
                "filed_airspeed_kts": 423,
                "filed_airspeed_mach": "",
                "filed_altitude": 250,
                "route": "ODI342042 BDF MOTIF4",
                "actualdeparturetime": 1321557900,
                "estimatedarrivaltime": 1321561259,
                "actualarrivaltime": 1321561020,
                "diverted": "",
                "origin": "KEAU",
                "destination": "KMDW",
                "originName": "Chippewa Valley Rgnl",
                "originCity": "Eau Claire, WI",
                "destinationName": "Chicago Midway Intl",
                "destinationCity": "Chicago, IL"
            },
            {
                "ident": "FIV576",
                "aircrafttype": "C56X",
                "filed_ete": "02:23:00",
                "filed_time": 1321479852,
                "filed_departuretime": 1321482600,
                "filed_airspeed_kts": 415,
                "filed_airspeed_mach": "",
                "filed_altitude": 400,
                "route": "COATE LHY J36 FNT J106 GRB",
                "actualdeparturetime": 1321488300,
                "estimatedarrivaltime": 1321497097,
                "actualarrivaltime": 1321496880,
                "diverted": "",
                "origin": "KTEB",
                "destination": "KEAU",
                "originName": "Teterboro",
                "originCity": "Teterboro, NJ",
                "destinationName": "Chippewa Valley Rgnl",
                "destinationCity": "Eau Claire, WI"
            },
            {
                "ident": "FIV576",
                "aircrafttype": "C56X",
                "filed_ete": "00:19:00",
                "filed_time": 1321470255,
                "filed_departuretime": 1321479000,
                "filed_airspeed_kts": 386,
                "filed_airspeed_mach": "",
                "filed_altitude": 40,
                "route": "MXE V3 SBJ",
                "actualdeparturetime": 1321475400,
                "estimatedarrivaltime": 1321476651,
                "actualarrivaltime": 1321476420,
                "diverted": "",
                "origin": "KPHL",
                "destination": "KTEB",
                "originName": "Philadelphia Intl",
                "originCity": "Philadelphia, PA",
                "destinationName": "Teterboro",
                "destinationCity": "Teterboro, NJ"
            }
        ]
    }
}


Thank you for taking the time to reply.

That JSON output looks as I would expect and is entirely valid.

I don’t know if NSJSONSerialization is able to parse and navigate all types of JSON or not–it’s possible but unexpected that it cannot handle some complex structures. Can you include the Objective C code that you’re trying to use to loop over the flights?

I do know that this other Open Source JSON framework for Objective C can be successfully used to parse and navigate the FlightXML JSON responses: code.google.com/p/json-framework/

Update: It appears that project is now available at github.com/stig/json-framework/

It very well could relate to the complexity. I tried a more rudimentary dictionary and the results rendered correctly. I’ll try that other framework again and let you know how I made out. Thanks again.

A quick follow-up after spending more time getting the new JSON support within iOS5 working. The original error was more user error than anything, understanding the hierarchy of FlightXML JSON results. You must convert both the outermost singleton element -and- the inner elements (each “flight” segment in this case) to NSDictionary as follows:



(void)fetchedData:(NSData *)responseData {

    NSError* parseError;
    //parse out the json data
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&parseError];  // All JSON
    NSDictionary* latestFlights = [json objectForKey:@"FlightInfoResult"]; // outer singleton element or "container"
    
    // Now depending on the complexity of the JSON data received, parse JSON as either NSArray (of individual flights in this case)
    // or NSDictionary for a single flight to get to the atomic elements within
    NSArray *flights = [latestFlights objectForKey:@"flights"];
     
    // 1) Parse elements of a single flight into an NSDictionary
    NSDictionary* singleFlight = [flights objectAtIndex:1];  // index "1" here could be passed in parameter as well
    
    // Now that we have chosen a single flight array, we can access individual elements from within
    // using this format:  [singleFlight objectForKey:@"<atomic element>"];
    
    // 3) Set the label appropriately
    humanReadble.text = [NSString stringWithFormat:@"Aircraft: %@ route %@ >> end >>",
                         [singleFlight objectForKey:@"ident"],
                         [singleFlight objectForKey:@"route"]
                         ];
    
    //build an info object and convert to json
    NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                          [singleFlight objectForKey:@"ident"], @"aircraft",
                          [singleFlight objectForKey:@"route"], @"route",
                          nil];
    
    //convert object to data
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info 
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    //print out the data contents
    jsonSummary.text = [NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];
     }


Obviously the URL is initialized elsewhere. Thanks to @bovineone and the tutorial(s) found on raywenderlich.com/5492/worki … n-in-ios-5 this code is working perfectly at this point.

I’m glad you got it working. Thanks for posting your results.