Iphone 如何获取JSON值?

Iphone 如何获取JSON值?,iphone,ios,objective-c,Iphone,Ios,Objective C,答复: { "AT": null, "EMs": [ { "EC": null, "LM": null, "SCs": 0, "SM": "Username or Password is invalid." } ], "OS": 1, "PWD

答复:

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

我希望这能奏效:

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

我希望这能奏效:

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"];
试着这样,

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

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

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

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

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

    // do something with SMString
}

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

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

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

    // do something with SMString
}
试试这个:

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);
试试这个:

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);
请尝试以下代码:

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

}
NSArray*EMsArray=[json objectForKey:@“EMs”];
对于(int i=0;i请尝试以下代码:

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

}
NSArray*EMsArray=[json objectForKey:@“EMs”];

对于(inti=0;i,正如我在json中看到的,它包含一个带有标记“EMs”的数组

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

objectForkey 
属性,并获取您想要的消息

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

请参阅json库,因为我可以在您的json中看到它包含一个带有标记“EMs”的数组

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

objectForkey 
属性,并获取您想要的消息

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

参考json库

最简单、最优雅的解决方案是像这样使用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."
)

最简单、最优雅的解决方案是像这样使用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."
)

下面的代码可能会帮助您

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

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

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

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

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

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

我假设每次您在响应数组中只得到一个对象。

Its in Phonegap代码。?json(NSDictionary)的响应是什么?请详细说明您的问题,然后我们将能够帮助您获得Its in Phonegap代码。?json(NSDictionary)的响应是什么?请详细说明您的问题,然后我们将能够帮助您