gpt4 book ai didi

asp.net - 我如何从 json 中获取值并在 NSLog 中显示它们

转载 作者:行者123 更新时间:2023-11-29 04:37:22 25 4
gpt4 key购买 nike

你好,我为 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""}";

最佳答案

进行这种嵌套日志记录:

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

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

关于asp.net - 我如何从 json 中获取值并在 NSLog 中显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com