从NSDictionary获取价值不起作用(get value from NSDictionary not work)

编程入门 行业动态 更新时间:2024-10-27 13:30:39
从NSDictionary获取价值不起作用(get value from NSDictionary not work)

1.我从Web服务获取JSON数据并将其添加到NSDictionary

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

然后我在NSLog中显示“dic”它出现了

{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}

然后我从“dic”获取数据值到新的NSDictionary“dt”

NSDictionary *dt = [dic objectForKey: @"query"];

然后我在NSLog中显示“dt”它出现了

({ID = 01;IndexNo = 1;Picture = "img/food-48.png";})

我想从“dt”获得“ID”。 我用这个代码

NSString *ID = [dt objectForKey: @"ID"];

但这是错误的

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a40a60

1.I get JSON data from web services and add It's to NSDictionary

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

2.Then I Show "dic" in NSLog It's present

{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}

3.Then I get data value from "dic" in to new NSDictionary "dt"

NSDictionary *dt = [dic objectForKey: @"query"];

4.Then I Show "dt" in NSLog It's present

({ID = 01;IndexNo = 1;Picture = "img/food-48.png";})

5.I want to get "ID" from "dt". I use this code

NSString *ID = [dt objectForKey: @"ID"];

but it's error

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a40a60

最满意答案

你能试试吗?

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSArray *arr = [dic objectForKey: @"query"]; NSDictionary *dt = arr[0]; NSString *ID = [dt objectForKey: @"ID"];

Can you try this

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSArray *arr = [dic objectForKey: @"query"]; NSDictionary *dt = arr[0]; NSString *ID = [dt objectForKey: @"ID"];

更多推荐

本文发布于:2023-08-01 18:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1363805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   价值   NSDictionary   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!