0

This is my code for parsing the json service. But I am getting nil value in json dictionary. But when I copied the json response and make a .txt file and parse it. It works well. strange issue.

-(void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:@"http://www.checker.co.il/testing/c_pda-load_reports_list.php?app=1"];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

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

This type of response,I am getting.

{"204281":{"rpID":"204281","ReportTitle":"Main Performance 
Analysis","ReportOrder":"2","URL":"report-network-status.php?ClientID=106&BranchID=&SetID=&
PropID=&props=&reportDateArng=3&hdrf_chosenCycleExtent=7&rangeStartd=0&rangeStartm=0&
rangeStarty=0&rangeEndd=0&rangeEndm=0&rangeEndy=0&pastDateCyclesNumber=2&selectedCycleExtent=&
showClientColorGroup=1&displayType=3&DyChartType=Bar2D&DycX=400&DycY=350&isPopup=0&
callingReportPageName=report-network-status.php&ReportTitle=\"%20
\"","ReportCategory":"Performance","CategoryRow":"0","CategoryCol":"4"},"204282":
{"rpID":"204282","ReportTitle":"Performance According to 
Ownership","ReportOrder":"5","URL":"report-property.php?ClientID=106&SetID=&PropID=78&
PropForFiltering=&props=&WorkerID=0&watchedReviews=&reportDateArng=1&hdrf_chosenCycleExtent=&
rangeStartd=0&rangeStartm=0&rangeStarty=0&rangeEndd=0&rangeEndm=0&rangeEndy=0&
pastDateCyclesNumber=2&selectedCycleExtent=&displayType=3&DyChartType=Column2D&DycX=400&DycY=250&
callingReportPageName=report-property.php&js_refresh=0&js_submit=0&
isPopup=0","ReportCategory":"Performance","CategoryRow":"0","CategoryCol":"4"}}

Thanks In Advance. :)

Bug
  • 2,576
  • 2
  • 21
  • 36
Rythm
  • 191
  • 1
  • 16
  • 1
    [NSData dataWithContentsOfURL:@"http://www.checker.co.il/testing/c_pda-load_reports_list.php?app=1"], it should be [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.checker.co.il/testing/c_pda-load_reports_list.php?app=1"]] – dthien Feb 15 '13 at 01:54
  • The error parm is there for a reason. Use it. – Hot Licks Feb 15 '13 at 02:10
  • 1
    BTW, there's no need to do the JSON parsing in the UI thread. – Hot Licks Feb 15 '13 at 02:32
  • On top of the other issues, does the URL require authentication? – Rob Feb 15 '13 at 03:28
  • @Rob Yes.. It requires authentication.. So what should I do?? – Rythm Feb 15 '13 at 07:11

1 Answers1

1
  1. dthien is right, that you need to use NSURL.

  2. danh is right, that you should look at the error. You can look at error.localizedDescription, but that's not always helpful. You might be getting error.code 3840, and if you look at the error.userInfo dictionary, you'll might see a description that says something like "JSON text did not start with array or object and option to allow fragments not set."

  3. Whenever you get an error in response to JSONObjectWithData, you should look at the data. So, before the JSONObjectWithData, do a:

    NSLog(@"%@", [[NSString alloc] initWithData:responseData
                                       encoding:NSUTF8StringEncoding]);
    
  4. When you do that, you may notice that the data does not contain the JSON string. You may have an authentication issue and you might want to use a NSURLConnection rather than just dataWithContentsOfURL, because the former gives you more detailed control over the connection. The correct next steps depends upon the nature of the authentication problem. There are two basic types of authentications:

    • Challenge-response authentication: If you had this sort of authentication and you pulled up the JSON URL in your web browser, you'd get some pop-up window asking for user credentials. Programmatically, if you wrote a didReceiveAuthenticationChallenge, you'd see it will be called. Anyway, if this is your type of authentication, see Authentication Challenges in the URL Loading System Programming Guide.

    • HTML-based authentication: You can tell if this is the case if (a) your response is a HTML login page; or (b) if your willSendRequest method is called as your request is redirected to a login page. If this is the case, the correct handling of this depends a little upon how the server programmers have handled authentication issues, as well as how you want to handle it (e.g. do you want to show the user an authentication page? do you want to use some previously established credentials? etc.).

    If you look at the contents of that responseData, the problem will be self evident. If you get something that looks like a HTML login page, then you're probably dealing with the latter.

Rob
  • 415,655
  • 72
  • 787
  • 1,044