0

Possible Duplicate:
How to parse JSON in iOS App

I have the following json:

NSString *jsonResult = @"{\"code\":\"000\",\"message\":\"success\",\"datas\":[{\"datas\":[{\"service_name\":\"user_info\",\"name\":\"Account Test 32\",\"number\":123,\"company\":\"ABC\",\"currency\":\"USD\",\"balance\":\"1000\",\"profit\":\"-80.00\",\"credit\":\"0.00\",\"equity\":\"1000.00\",\"leverage\":100,\"free_margin\":\"1000.00\",\"free_margin_mode\":1.0,\"server_name\":\"localhost\",\"stopout_level\":5,\"stopout_mode\":0}],\"service_name\":\"user_info\"}]}";

Can anyone give an example how to parse this?

Community
  • 1
  • 1

2 Answers2

2

Try this. Import SBJson parser you could use TouchJson also but I prefer SBJson.

#import "JSON.h"

// Create SBJSON object to parse JSON
SBJSON *parser = [[SBJSON alloc] init];

// parse the JSON string into an object - assuming json_string is a NSString of JSON data
NSDictionary *object = [parser objectWithString:jsonResult error:nil];
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • how do i get the value inside the datas array ? – user772315 Nov 04 '11 at 13:15
  • You have got your `jsonResult` right? pass that to the JSON parser. It will convert to ObjectiveC objects... that's what you need right? So what i have written is the parser part. Where you import the json parser & parse the string.... – Srikar Appalaraju Nov 04 '11 at 13:16
  • It still got me an error: SBJsonParser *json = [[SBJsonParser alloc] init]; NSError *jsonError; NSDictionary *parsedJSON = [json objectWithString:jsonResult error:&jsonError]; NSDictionary* jsonValue = [parsedJSON objectForKey:@"code"]; NSLog(@"code: %@", [jsonValue objectForKey:@"code"]); – user772315 Nov 04 '11 at 13:22
  • so its working? why posting the code? – Srikar Appalaraju Nov 04 '11 at 13:23
  • Not working. sorry for posting the code. – user772315 Nov 04 '11 at 13:25
  • How is it not working? Its simple. For more indepth steps refer here - http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c – Srikar Appalaraju Nov 04 '11 at 13:29
1

Your best option is using a JSON parsing library such a TouchJson.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292