0

I have a WebService, which give me the fallowing Json-String back:

"{\"password\" : \"1234\",  \"user\" : \"andreas\"}"

I call the webservice and try to parse the returned data like:

[NSURLConnection sendAsynchronousRequest: request
                                   queue: queue
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {


        if (error || !data) {
           // Handle the error
        } else {
           // Handle the success
           NSError *errorJson = nil;
           NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &errorJson];
           NSString *usr = [responseDict objectForKey:@"user"];
        }
   }
 ];

But the resulting NSDictionary looks like:

enter image description here

What has the effect, that I cannot get the values - for example user. Can someone help me, what I am doing wrong? - Thank you.

BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • How did you determine what the JSON string is? – Wain Jun 24 '13 at 19:08
  • Apparently it is a dictionaty in a dictionary. – Hermann Klecker Jun 24 '13 at 19:10
  • Evidently that's not precisely what the webservice gives you. What is the value of `data` (preferably converted to a string) before being parsed? – Kevin Jun 24 '13 at 19:12
  • You are right! - I am doing: `[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string doSomething(string email, string name) { return "{\"password\" : \"1234\", \"user\" : \"andreas\"}"; }` and get: "{d":"{\"password\" : \"1234\", \"user\" : \"andreas\"}"}" but why? – BennoDual Jun 24 '13 at 19:25
  • After reading this thread, it seems you were lying. What you said the WebService was returning wasn't actually what it returned, it was what you thought it was returning. Not the same thing. – gnasher729 Mar 27 '14 at 12:11

1 Answers1

1

From the debugger screenshot is seems that the server is (for whatever reason) returning "nested JSON": responseDict[@"d"] is a string containing JSON data again, so you have to apply NSJSONSerialization twice:

NSError *errorJson = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options:0 error: &errorJson];
NSData *innerJson = [responseDict[@"d"] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *innerObject = [NSJSONSerialization JSONObjectWithData:innerJson options:NSJSONReadingMutableContainers error:&errorJson];
NSString *usr = [innerObject objectForKey:@"user"];

If you have the option, a better solution would be to fix the web service to return proper JSON data.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • You are right. But I don't know, why I became the wrong result from the WebService. It is a ASP.NET Webservice and there I do only: `return "{\"password\" : \"1234\", \"user\" : \"andreas\"}";` – BennoDual Jun 24 '13 at 19:33
  • @ThomasKehl: I can't answer that, I don't know anything about ASP.NET. – Martin R Jun 24 '13 at 19:35
  • @ThomasKehl: This is apparently [just something ASP.NET does](http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/) when handling AJAX requests. – Chuck Jun 24 '13 at 19:52