0

I send some credentials to a web-service.

I can get the responce as NSData or as an NSString.

Whats the simpleist way to convert the (NSData or NSString)JSON to a NSDictionary so I can process it?

Building for iOS4.3 Many Thanks, -Code

  • **First duplicate:** `NSData` to `NSString` => [Here](http://stackoverflow.com/questions/6411965/converting-nsdata-to-nsstring-in-objective-c). **Second duplicate:** "JSON" to `NSDictionary` => [Here](http://stackoverflow.com/questions/5038371/convert-json-feed-to-nsdictionary). – Rui Peres Jul 05 '12 at 07:48

4 Answers4

1

From iOS 5.0 and on you can use NSJSONSerialization, docs here.

There is a good tutorial on how to use it, here.

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
0

Use this :

NSString *urlString = [NSString stringWithFormat:kURLString,self.chapter,self.page];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[urlString dataUsingEncoding:NSISOLatin1StringEncoding]];

NSData* responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding];


NSDictionary* result = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Result"];
aks.knit1108
  • 1,305
  • 9
  • 20
  • What lib is the method JSONValue from? –  Jul 05 '12 at 08:01
  • You have to add JSON classes in your project and for this download it from gitHub and then #import "JSON.h" in your required class where you want to parse data. – aks.knit1108 Jul 05 '12 at 08:38
0

I use SBJson

NSArray *arrObj = (NSArray*)[[JsonParser sharedJsonParser] getObjectsFromJsonString:responseString];

response string is a JSON string here

Prabhat Kasera
  • 1,129
  • 11
  • 28
0

Download SBJsonParser Framework using the link, http://github.com/stig/json-framework/downloads and add required classes to your project (SBJsonParser.h).

Import SBJsonParser.h and use the following code,

    SBJsonParser *parser = [[SBJsonParser alloc] init];

     //dictionary with name,id
     NSDictionary *jsonObject = [parser objectWithString:jsonString error:NULL];  

     //name array                
      NSMutableArray *nameArray = [[NSMutableArray alloc]init];
   for (NSMutableDictionary *dic in jsonObject){

      [nameArray addObject:[dic valueForKey:@"name"]];
    }
Ab'initio
  • 5,368
  • 4
  • 28
  • 40