cra*_*gen 12 iphone json objective-c nsmutablearray ios
我来自JSON
[{ "照片":空}]
我使用这段代码
NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]];
Run Code Online (Sandbox Code Playgroud)
如果我想使用if(),我怎么检查它?
编辑
这是JSON数据
[{"photo":
[{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
]
Run Code Online (Sandbox Code Playgroud)
这是我的JSON解析器
NSError *theError = nil;
NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
小智 37
我相信大多数JSON解析器都表示null
为[NSNull null]
.
考虑jsonDict
到数组中单个元素的指向,那么以下内容应该有效:
if ([jsonDict objectForKey:@"photo"] == [NSNull null]) {
// it's null
}
Run Code Online (Sandbox Code Playgroud)
根据评论进行编辑:因此jsonDict
,尽管名称是一个数组.在这种情况下,重命名jsonDict
,以jsonArray
避免进一步的混乱.然后,考虑jsonArray
指向类似于问题中发布的示例的数组:
NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
if (photo == [NSNull null]) {
// photo is null
}
else {
// photo isn't null
}
}
Run Code Online (Sandbox Code Playgroud)
根据OP的修改问题进一步编辑:
NSArray *jsonArray = [string JSONValue];
NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
if (photo == [NSNull null]) {
// photo is null
}
else {
// photo isn't null. It's an array
NSArray *innerPhotos = photo;
…
}
}
Run Code Online (Sandbox Code Playgroud)
如果有效负载是具有可能值的复杂JSON结构,则宏可能很有用.
#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }
Run Code Online (Sandbox Code Playgroud)
和宏可以像引用一样
SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]);
Run Code Online (Sandbox Code Playgroud)
没有简单的方法来解决这个问题,但我这样做的方法是在NSObject上创建一个类别:
@interface NSObject (NotNull)
- (instancetype)notNull;
@end
Run Code Online (Sandbox Code Playgroud)
像这样实现:
@implementation NSObject (NotNull)
- (instancetype)notNull
{
return self;
}
@end
@implementation NSNull (NotNull)
- (instancetype)notNull
{
return nil;
}
@end
Run Code Online (Sandbox Code Playgroud)
然后你可以发送notNull
到JSON字典中的任何可选对象,nil
如果它是NSNull 你会回来的.否则你会得到原始对象.例如:
self.parentIdentifier = [dictionary[@"parent_id"] notNull];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17999 次 |
最近记录: |