0

回复:

{
        "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": "aa@gmail.com",
        "FN": "bb",
        "IsCon": true,
        "IsSuc": false,
        "SD": "bb",
        "SLT": "XU0QpDVC",
        "STs": 0,
        "UQId": "1d3346c",
        "US": 2,
        "Ver": "6.0"
    }

在这里,我想获取“用户名或密码无效”之类的 SM 标记值。

我试过这段代码

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"];
4

8 回答 8

2

我希望这会起作用:

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"];
于 2013-05-20T12:01:17.820 回答
1

您感兴趣的数据位于字典内的数组中,因此您需要执行以下操作:

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

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

    // do something with SMString
}
于 2013-05-20T12:03:28.913 回答
0

正如我在您的 json 中看到的那样,它包含一个带有标签“EMs”的数组。

因此,您首先需要从 json 中提取数组并需要在该数组的 inde 0 处获取字典。然后你可以简单地使用

objectForkey 

nsdictionary 的属性并获取您想要的消息。

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"]);
}

参考https://github.com/fbsamples/ios-friend-smash/tree/master/friendsmash_complete/friendSmasher/Classes获取 json 库

于 2013-05-20T12:14:02.190 回答
0

试试这个代码:

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);

}

希望这可以帮助!

于 2013-05-20T12:11:03.653 回答
0

尝试这个:

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);
于 2013-05-20T12:07:48.687 回答
0

试试这样

    NSArray *EMsArray = [json objectForKey:@"EMs"];
    if([EMsArray count]>0)
         NSString *SMString = [[EMsArray objectAtIndex:0] objectForKey:@"SM"];
于 2013-05-20T12:02:48.737 回答
0

最简单和优雅的解决方案是像这样使用 keypath:

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

见示例代码:

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

输出:

Value of key EMs -> SM: (
  "Username or Password is invalid."
)
于 2013-05-20T12:18:26.100 回答
0

以下代码可能对您有所帮助

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

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

我假设每次你只得到一个对象时,在你的响应数组中。

于 2013-05-20T12:31:19.450 回答