如何获取JSON值?

时间:2013-05-20 11:57:02

标签: iphone ios objective-c

回应:

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

8 个答案:

答案 0 :(得分: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"];

答案 1 :(得分:1)

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

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

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

    // do something with SMString
}

答案 2 :(得分:0)

尝试这样,

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

答案 3 :(得分: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);

答案 4 :(得分: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);

}

希望这有帮助!

答案 5 :(得分: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库

答案 6 :(得分:0)

最简单优雅的解决方案是使用这样的密钥路径:

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."
)

答案 7 :(得分: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"];

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