0

I am making a request to a server for some results that should be returned in JSON format.

When I print out the requested string it shows correct the right output. However when I try to parse the result string i get the error:

Error Domain=org.brautaset.JSON.ErrorDomain Code=3 "Object value expected for key: result" UserInfo=0x6c4cbe0 {NSUnderlyingError=0x6c4caf0 "Object value expected for key: map", NSLocalizedDescription=Object value expected for key: result}

This is my code of JSON :

SBJSON *jsonParser = [[SBJSON alloc] init];

[jsonParser setHumanReadable:YES];
NSError *theError;

NSString *loginPayload = @"{\"service\": \"getMyJobs\", \"params\": {\"map\": {\"day\": {\"javaClass\": \"java.sql.Timestamp\", \"time\": 1316757600000}}}, \"security\": {\"map\": {}}, \"userInfo\": false}";

NSURL * url = [NSURL URLWithString:kURLForAPI];

NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[loginPayload dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPShouldHandleCookies:YES];
[req setValue:@"text/xml  charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[req setValue:cookie forHTTPHeaderField:@"Cookie"];

NSHTTPURLResponse * response = nil;
NSData * dataResult = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&theError];
NSMutableDictionary *dicts;

if (dataResult) {
    NSString *resultString = [[NSString alloc] initWithData:dataResult encoding:NSUTF8StringEncoding];
    //NSLog(@"login response: %@",resultString);
    //NSLog(@"response %@", [response allHeaderFields]);

    dicts = [jsonParser objectWithString:resultString error:&theError];
    //dicts = [resultString JSONValue];
    if(theError)
    {
        NSLog(@"Result %@",[theError description]);
    }
DShah
  • 9,768
  • 11
  • 71
  • 127
bakwarte
  • 317
  • 1
  • 2
  • 10
  • If possible, show us your `resultString`; that would make providing an analysis easier. From the error, it appears you may have a problem with the empty "map" attribute that you are submitting. – jstevenco Dec 20 '11 at 21:08
  • I used jsonlint to validate the JSON and it's giving me an error with the date format. Here is the result string: { "taskDate": newDate("September 23, 2011 00:00:00"), "roleTypeId": "TECHNICIAN"} I am getting an error on the taskDate value – bakwarte Dec 21 '11 at 00:37
  • That does not look like a valid format for representing a date, at least as far as SBJSON goes. I'd expect to see the date as either a string literal which you can use `NSDate` and `NSDateFormatter` to massage as per your needs, or as a `time_t` integer. At least those are the ways that I've used Stig's framework to work with dates. So as far as what you have, omitting the `newDate()` enclosing the string literal and you should be good to go. – jstevenco Dec 21 '11 at 01:51

2 Answers2

0

Don't use NSString to create JSON request body. Use [NSDictionary dictionaryWithObjects:objects forKeys:keys]; and then just do [dictionaryinput JSONRepresentation]; It will create JSON string for you. This is the best way to serialization and deserialization JSON object and will save lot of trouble.

AAV
  • 3,785
  • 8
  • 32
  • 59
  • Yea I am only using that to test for now. The main problem is with my response value I have a returned key-value pair: "taskDate":newDate(000000) The JSON parser cannot handle it. How do I fix that? – bakwarte Dec 21 '11 at 01:22
  • Ask upstream to return JSON, rather than some custom JSON-like format. – Stig Brautaset Sep 03 '12 at 16:38
0

Create date in this format.

   NSStime *time = [NSString stringWithFormat:@"/Date(%@)/", [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970] * 1000]];
AAV
  • 3,785
  • 8
  • 32
  • 59