你好我为webservice创建了一个json String,我想输出名称和描述以在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""}";
答案 0 :(得分: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'") );
}
看看它是否有助于你弄清楚你的假设在哪个级别和哪个对象上。