从JSON数组objective-c获取值的问题
Posted
技术标签:
【中文标题】从JSON数组objective-c获取值的问题【英文标题】:problem getting a value from a JSON array objective-c 【发布时间】:2011-08-31 13:20:15 【问题描述】:我从 UPX 服务器接收 JSON 数组,但我无法获取值
我需要来自“relation_data”的对象“名称”,但我无法进入“relation_data”
我就是这样做的
NSLog(@"All of the arrays: %@", json);
NSDictionary *Allinfo = [json objectForKey:@"response"];//I go into "response"
NSLog(@"All with response array: %@", Allinfo);
NSDictionary *datainfo = [Allinfo objectForKey:@"data"];//i go into "data"
NSLog(@"Data : %@", datainfo);
但是我如何进入“relation_data”并获取“name”的值?
response = (
data = (
"business_data_rev" = 12;
"contact_set_rev" = 15;
currency = EUR;
"date_added" = "2011-08-31 12:10:59.338664+02";
"date_invoiced" = "2011-08-31 12:10:59.338664+02";
"date_modified" = "2011-08-31 12:11:04.45442+02";
"date_paid" = "2011-08-31 12:10:59.408335+02";
description = "";
id = 104;
name = "Invoice #104";
"person_rev" = 12;
realid = 104;
"relation_data" =
id = 12;
iscreditor = 0;
isdebitor = 1;
isprivate = 0;
name = "Emre.";
"subaccount_id" = 9;
;
)
【问题讨论】:
【参考方案1】:试试这个
[dataInfo valueForKeyPath:@"relation_data.name"];
【讨论】:
hmm,如果我这样做,我会在 1 个变量(例如字符串)中获得所有名称(104 条记录),我怎样才能得到 1 乘 1? 试试[[dataInfo objectAtIndex:0] valueForKeyPath:@"relation_data.name"]
谢谢解决这个问题当我添加字典然后设置valueforkeypath。以上是关于从JSON数组objective-c获取值的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Angular2(Typescript)中的 Json 数组中获取值的总和
当我只想获取字典中的特定值时,如何解码字典值的 JSON 数组?