加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NSJSONSerialization

发布时间:2020-12-16 19:58:02 所属栏目:百科 来源:网络整理
导读:- (IBAction)JOSNButtonPressed:(id)sender { 02 03 NSString *str=[@ "http://douban.fm/j/mine/playlist?type=nh=channel=0from=mainsiter=4941e23d79" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 04 05 NSURL *url = [NSURLURLWi

- (IBAction)JOSNButtonPressed:(id)sender {
02
03
NSString *str=[@"http://douban.fm/j/mine/playlist?type=n&h=&channel=0&from=mainsite&r=4941e23d79"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
04 05 NSURL *url = [NSURLURLWithString :str];
06 07 // 构造 ASIHTTPRequest 对象
08 09 ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
10 11 // 开始同步请求
12 13 [request startSynchronous];
14 15 NSError *error = [request error];
16 17 assert(!error);
18 19
20 21 // 如果请求成功,返回 Response // 对于2进制数据,使用:responseData
22 23 NSData *data = [request responseData];
24 25 NSString *tmp=[[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];
26 NSLog(@"%@",tmp);
27
28 [tmp release];
29 30
31 32 // 返回一个JSON对象
33 34 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
35 NSArray *songArray = [json objectForKey:@"song"];
36 37 "song:%@"38 39 /*
40 41 NSDictionary *aSony = [songArray objectAtIndex:0];
42 43 NSString *array = [aSony objectForKey:@"albumtitle"];
44 45 NSLog(@"albumtitle:%@",array);
46 47 */
48 49 // 遍历输出JSON里song里的albumtitle
50 51 for(NSDictionary *aSony in songArray)
52 53 {
54 55 NSString *array = [aSony objectForKey:@"albumtitle"56 57 "albumtitle:%@"58 59 }
60 61 }
查看源码
打印 ?
01 // 自己构建一个JSON
- (IBAction)createaJSON:(id)sender {
NSDictionary *song = [NSDictionarydictionaryWithObjectsAndKeys:@"i can fly""title""4012""length""Tom""Singer"// isValidJSONObject判断对象是否可以构建成json对象
if([NSJSONSerializationisValidJSONObject:song])
{
NSError *error;
// 创造一个json从Data,NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。
NSData *jsonData = [NSJSONSerializationdataWithJSONObject:song options:NSJSONWritingPrettyPrintederror:&error];
NSString *json =[[NSStringalloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
"json data:%@"}
}



#define kGlobalQueue    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define kDoubanUrl      @"http://douban.fm/j/mine/playlist?type=n&h=&channel=0&from=mainsite&r=4941e23d79"
-(void) loadJsonData:(NSURL *)url
{
    dispatch_async(kGlobalQueue,^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        [self performSelectorOnMainThread:@selector(parseJsonData:) withObject:data waitUntilDone:NO];
    });
}
-(void) parseJsonData:(NSData *)data
{
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (json == nil) {
        NSLog(@"json parse failed rn");
        return;
    }
    NSArray *songArray = [json objectForKey:song];
    NSLog(song collection: %@rn,songArray);

    _song = songArray;
    self.songIndex = 0;
    NSDictionary *song = [songArray objectAtIndex:song info: %@tn 
       
     

  Foundation对象转换为json数据

View Code
NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:i can fly",title4012lengthTomSingerif ([NSJSONSerialization isValidJSONObject:song])
    {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:song options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(json data:%@(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读