0

你好,我为 web 服务创建了一个 json 字符串,我想输出名称和描述以在 NSLog 中显示它。我怎样才能做到这一点。到目前为止,这是我的代码:

    dic = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:nil];


NSLog(@"Results %@",[NSString stringWithFormat:@"%@",[[dic objectForKey:@"d"]objectForKey:@"Name"]]);

我收到此错误:

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6b50f30

当我在我的字典中创建一个 NSLog 时,我得到了这个:

{
d = "{
\n  \"Name\": \"Apple\",
\n  \"Beschreibung\": \"Steve Jobs ist tot\"}";
}

我来自 werbservice 的 json 字符串如下所示:

string json = @"{
""Name"": ""Apple"",
""Beschreibung"": ""Steve Jobs ist tot""}";
4

1 回答 1

1

做这种嵌套日志记录:

NSLog(@"Results %@",[NSString stringWithFormat:@"%@",[[dic objectForKey:@"d"]objectForKey:@"Name"]]);

真的很棘手。我猜想返回的任何对象“d”不一定是 NSDictionary 对象,也许是 NSArray ?

尝试这样的事情:

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:nil];

// this gives the whole NSDictionary output:
NSLog( @"Results %@", [dic description] );

// get the dictionary that corresponds to the key "d"
NSDictionary * dDic = [dic objectForKey: @"d"];
if(dDic)
{
    NSString * nameObject = [dDic objectForKey: @"Name"];
    if(nameObject)
    {
        NSLog( @"object for key 'Name' is %@", nameObject );
    } else {
        NSLog( @"couldn't get object associated with key 'Name'" );
    }
} else {
    NSLog( @"couldn't get object associated with key 'd'") );
}

看看它是否能帮助你弄清楚你的假设在哪个级别和哪个对象上被打破了。

于 2012-06-05T09:24:39.977 回答