【问题标题】:NSJSONSerialization IssueNSJSON序列化问题
【发布时间】:2012-06-15 05:44:56
【问题描述】:

您好,我有这个代码。

NSString *infoString = [NSString stringWithFormat:@"http://link.com/post.php?name=%@&street=%@&city=%@&state=%@&zip=%@&lat=%@&lon=%@", name, street, city, state, zip, str1, str2];
        NSURL *infoUrl = [NSURL URLWithString:infoString];
        NSData *infoData = [NSData dataWithContentsOfURL:infoUrl];

        NSError *error;
        responseDict = [NSJSONSerialization JSONObjectWithData:infoData options:0 error:&error];
        NSString *responseString = [responseDict copy];
        NSLog(@"responseDict: %@", responseString);

在控制台中显示。

2012-06-14 22:37:04.022 PartyApp[20221:fb03] responseDict: {
    status = 1;
}

无论如何我可以做一个 if 语句来表明如果值为 1 则执行此操作,如果不是则执行此操作。我在下面尝试过。

if ([responseDict objectForKey:@"status = 1"]) {
            Then do this
        }

但它不起作用,我做错了什么或者我能做什么?

【问题讨论】:

    标签: iphone xcode ios5 nsjsonserialization


    【解决方案1】:
    if ([responseDict objectForKey:@"status = 1"]) {
                Then do this
            }
    

    应该是..

    if ([responseDict objectForKey:@"status"]) {
                //this will be done in case of 1
            }else{
    //this will be done in case of 0
    

    }

    只有status 是关键,而不是你的全部。希望这会有所帮助。

    更新:以上几行就足够了,因为它返回 1,否则将继续,如果为 0,则条件为假,并将转到 else 子句

    【讨论】:

    • 我如何知道应用返回的是 0 还是 1?
    • 它确实有效,我希望我能,但它说我需要 15 声望。
    • @DewanPayroda 你可以通过点击问题旁边的勾号来接受它作为答案.. :)
    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多