【发布时间】:2016-10-16 23:38:27
【问题描述】:
我返回了我正在解析的 JSON 数据集。
如果数据包含一条记录,它看起来像这样:
dictData: {
Recordset = (
{
Record = (
{
MyDate = "2016-06-15 11:04:43";
MyID = 53070;
SomeDescription = "";
}
);
}
);
}
如果数据不包含一条记录,它看起来像这样: 请注意返回集中导致我出现问题的“”。
dictData: {
Recordset = (
""
);
}
我无法计算 Recordset 中的零长度数组。
这是我解析数据的基本代码。
NSDictionary * dictData = [NSDictionary dictionaryWithDictionary:[someOtherDictionary objectForKey:@"Data"]];
NSArray * arrRecordSet = [dictData objectForKey:@"Recordset"];
if([arrRecordSet objectAtIndex:0] != nil) {
NSLog(@"CONTAINS RECORDS");
//Do something with the records
}
else {
NSLog(@"DOES NOT CONTAIN RECORDS");
}
我尝试了各种迭代,例如检查 [arrRecordSet objectAtIndex:0] 长度是否大于零或检查计数大小。
我认为双引号让我失望。
有什么帮助吗?
更新
请求的原始 JSON
一个记录
{
APIResponse = {
Data = {
Recordset = (
{
Record = (
{
MyDate = "2016-06-15 11:04:43";
MyID = 53070;
SomeDescription = "";
}
);
}
);
};
};
}
没有记录
{
APIResponse = {
Data = {
Recordset = (
""
);
};
};
}
【问题讨论】:
-
当你记录 arrRecordSet.count 时它会说什么?
-
它显示计数为 1。如果返回 1 条记录,它也显示计数为 1。
-
你能以一个有效的 JSON 为例,而不是你解析的 nsdictionary 的控制台日志吗?
-
@arturdev 我已更新问题以包含原始 JSON
-
设计服务器端的人根本不是专业人士,但你可以做的是检查
"Recordset"的数组是否有String项,如果没有,那么你可以检查Dictionary项目。
标签: ios json nsarray nsdictionary nsmutabledictionary