0
votes

Response:

{
        "AT": null,
        "EMs": [
            {
                "EC": null,
                "LM": null,
                "SCs": 0,
                "SM": "Username or Password is invalid."
            }
        ],
        "OS": 1,
        "PWD": "3456",
        "UId": 399,
        "UN": "bb",
        "COM": "Apple",
        "DId": 0,
        "DN": "iPhone",
        "DOS": "iOS",
        "EA": "[email protected]",
        "FN": "bb",
        "IsCon": true,
        "IsSuc": false,
        "SD": "bb",
        "SLT": "XU0QpDVC",
        "STs": 0,
        "UQId": "1d3346c",
        "US": 2,
        "Ver": "6.0"
    }

Here I want to fetch SM tag value like "Username or Password is invalid."

I have tried this code

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSString *SMString = [json objectForKey:@"SM"];
NSString *OSString = [json objectForKey:@"OS"];
NSString *DIdString = [json objectForKey:@"DId"];
NSString *UIdString = [json objectForKey:@"UId"];
8
Its in Phonegap code.? - Akshay Joy
what is the response for json (NSDictionary)? - Niru Mukund Shah
Please elaborate your question clearly after this we will be able to help you - iEinstein

8 Answers

2
votes

I would expect this to work:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions
                                                       error:&error];
NSArray *EMsArray = [json objectForKey:@"EMs"];
NSAssert([EMsArray count] > 0, @"Expected at least one element in EMs array");
NSString *SMString = [[EMsArray objectAtIndex:0] objectForKey:@"SM"];
1
votes

The data you're interested in is in an array inside the dictionary, so you need to do something like:

NSArray *EMsArray = [json objectForKey:@"EMs"];

for (NSDictionary *EMsDictionary in EMsArray) {
    NSString *SMString = [EMsArray objectForKey:@"SM"];

    // do something with SMString
}
0
votes

Try this:

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSArray *EMs = [json objectForKey:@"EMs"];
NSDictionary *dict = [EMS objectAtIndex:0];
NSString *str = [dict objectForKey:@"SM"];

NSLog(@"%@",str);
0
votes

try like this,

    NSArray *EMsArray = [json objectForKey:@"EMs"];
    if([EMsArray count]>0)
         NSString *SMString = [[EMsArray objectAtIndex:0] objectForKey:@"SM"];
0
votes

Try this code:

NSArray *EMsArray = [json objectForKey:@"EMs"];

for (int i=0;i<[EMsArray count];i++) {

NSDictionary *dicObj = [EMsArray objectAtIndex:i]
NSString *str = [dicObj objectForKey:@"SM"];

NSLog(@"JSON value is:%@",str);

}

Hope this helps!

0
votes

As i can see in your json that it contains an array with tag "EMs".

So you first need to extract array from json and Need to fetch the dictionary at inde 0 of that array. Then you can simple use

objectForkey 

property of nsdictionary and fetch the message you want to.

SBJSON *json = [SBJSON new];

NSError *error = [[NSError alloc] init];

 NSString *jsonString = [json objectWithString:"your json string" error:&error];

NSArray *array = [[NSArray alloc]initWithArray:[jsonString valueForKeyPath:@"EMs"]];

if([array count]>0)
{
    NSDictionary *_dic = [array objectAtIndex:0];
    NSLog(@"%@",[_dic objectForKey:@"SM"]);
}

refer https://github.com/fbsamples/ios-friend-smash/tree/master/friendsmash_complete/friendSmasher/Classes for json library

0
votes

Most easy and elegant solution is to use keypath like this:

NSString *SMString = [json valueForKeyPath:@"EMs.SM"];

See example code:

NSString *SMString = [dict valueForKeyPath:@"EMs.SM"];
NSLog(@"Value of key EMs -> SM: %@", SMString);

which outputs:

Value of key EMs -> SM: (
  "Username or Password is invalid."
)
0
votes

following code may help you

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

NSMutableArray *EmsArray=[json objectForKey:@"EMs"];
NSMutableDictionary *dataDic=[EMsArray objectAtIndex:0];
NSString *smString=[dataDic objectForKey:@"SM"];

i assume that in your response array every time you get only one object.