现在的位置: 首页 > 综合 > 正文

json数据解析

2018年05月12日 ⁄ 综合 ⁄ 共 1017字 ⁄ 字号 评论关闭

NSDictionary* myInfo =
    [NSDictionary dictionaryWithContentsOfJSONURLString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
    NSLog(@"1111111%@",myInfo);
    
    
    NSDictionary* information =
    [NSDictionary dictionaryWithObjectsAndKeys: @"orange",@"apple",@"banana",@"fig",nil];
    NSData* json = [information toJSON];

@interface NSDictionary(JSONCategories)

+(NSDictionary*)dictionaryWithContentsOfJSONURLString: (NSString*)urlAddress;
-(NSData*)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString: (NSString*)urlAddress
{
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: urlAddress] ];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}
-(NSData*)toJSON
{
    NSError* error = nil;
    id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}
@end

抱歉!评论已关闭.