-4

I have an application in which I am having a json response like this. {"success":"true","message":"You have logged in","pserial":"1"} and I am separating with ":". And I am getting data like this pSerial:"1"} but I want only 1 value.

NSURL *url = [NSURL URLWithString:strUrl];
NSData *respData = [NSData dataWithContentsOfURL:url];
NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];
NSString *approvalString = [[strResp componentsSeparatedByString:@":"] objectAtIndex:3];
NSLog(@"pSerial:%@",approvalString);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
vashi
  • 17
  • 5

5 Answers5

3

for Example :

SBJsonParser *jsonPar = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObj = [jsonPar objectWithString:jsonString error:&error];


id jsonObj = [jsonPar objectWithString:jsonString error:&error];

if ([jsonObj isKindOfClass:[NSDictionary class]])
    // treat as a dictionary, or reassign to a dictionary ivar
else if ([jsonObj isKindOfClass:[NSArray class]])
    // treat as an array or reassign to an array ivar.

Then get the value :

NSMutableArrary *userMutArr = [NSMutableArray array];

for (NSDictionary *dict in jsonObj)
{
    User *userObj = [[[User alloc] init] autorelease];
    [userObj setFirstName:[dict objectForKey:@"firstName"]];
    [userObj setLastName:[dict objectForKey:@"lastName"]];
    [userObj setAge:[[dict objectForKey:@"age"] intValue]];
    [userObj setAddress:[dict objectForKey:@"address"]];
    [userObj setPhoneNumbers:[dict objectForKey:@"phoneNumber"]];

    [userMutArr addObject:userObj];
}

Hope you will understand. and read some Documents. it will help you.

Sandeep Sachan
  • 373
  • 3
  • 15
1

It looks like you are in need of JSON Parsing. Here, what you want is JSON Parsing, not separating data from the JSON Response. JSON is the format of the data in which data is formatted in Key-Value pairs. You can fetch the "Value" of any object using the "Key".

Your first two lines are correct.

NSURL *url = [NSURL URLWithString:strUrl];
NSData *respData = [NSData dataWithContentsOfURL:url];

Now, you can parse JSON Response like this :

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:respData options:kNilOptions error:&error];
NSString *pSerial = [json objectForKey:@"pserial"];

This will give you the value of "pserial" from your response. Similarly, you can get the values for "success" and "message". You can check it using this line :

NSLog(@"pserial :: %@",pserial);
Bhavin
  • 27,155
  • 11
  • 55
  • 94
0

You need to parse the JSON Response String, you can use any JSON parser like:

https://github.com/stig/json-framework/

And in your code do:

NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];

NSDictionary *ResponseDictionary = [strResp JSONValue];

NSString * pSerial = (NSString*)[ResponseDictionary objectForKey:@"pserial"];
Jamal Zafar
  • 2,179
  • 2
  • 27
  • 32
0

Dont separation by ":" just use JSONValue your response is like

 //     {"success":"true","message":"You have logged in","pserial":"1"} 

 //  with SBJsonParser parse your object like this

 NSDictionary *responseJson = [YOUR-OBJECT JSONValue];

Note: dont forget to add Json header file

Irshad Mansuri
  • 397
  • 2
  • 12
0

Its better you use any OpenSource Json Parser

Here is a stack post Comparison of different Json Parser for iOS

enter image description here

iOS Coder
  • 39
  • 1
  • 2
  • 4