• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            厚積薄發(fā),滴水穿石

            搬家到主站了:http://www.cnblogs.com/cokecoffe/
            隨筆 - 45, 文章 - 8, 評(píng)論 - 12, 引用 - 0
            數(shù)據(jù)加載中……

            IOS5 JSON

            IOS5.0開(kāi)始支持JSON,所以不用第三方的解析了。

            //Foundation(NSDictionaryNSData) 轉(zhuǎn)換為JSon格式的NSData 用來(lái)發(fā)送

            //+ dataWithJSONObject:options:error:

             

            //JSON格式的Data轉(zhuǎn)換為Foundation(NSDictionaryNSData)   用來(lái)解析

            //+ JSONObjectWithData:options:error:

            code:

            https://github.com/cokecoffe/ios-demo/tree/master/HTTP

            1. 解析json成dic對(duì)象
            2. -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error;
            3. NSDictionary* json =[NSJSONSerialization
            4. JSONObjectWithData:responseData //1
            5. options:kNilOptions
            6. error:&error];
            7. NSArray* latestLoans =[json objectForKey:@"loans"]; //2
            8. NSLog(@"loans: %@", latestLoans); //3
            9. }
            10. 把對(duì)象生成json string
            11. //build an info object and convert to json
            12. NSDictionary* info =[NSDictionary dictionaryWithObjectsAndKeys:[loan objectForKey:@"name"],
            13. @"who",
            14. [(NSDictionary*)[loan objectForKey:@"location"]
            15. objectForKey:@"country"],
            16. @"where",
            17. [NSNumber numberWithFloat: outstandingAmount],
            18. @"what",
            19. nil];
            20. //convert object to data
            21. NSData* jsonData =[NSJSONSerialization dataWithJSONObject:info
            22. options:NSJSONWritingPrettyPrinted error:&error];
            23. //print out the data contents
            24. jsonSummary.text =[[NSString alloc] initWithData:jsonData
            25. encoding:NSUTF8StringEncoding];
            26. 添加json方法至dic
            27. @interfaceNSDictionary(JSONCategories)
            28. +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
            29. -(NSData*)toJSON;
            30. @end
            31. @implementationNSDictionary(JSONCategories)
            32. +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress{
            33. NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
            34. __autoreleasing NSError* error =nil;
            35. id result =[NSJSONSerialization JSONObjectWithData:data
            36. options:kNilOptions error:&error];
            37. if(error !=nil)returnnil;
            38. return result;
            39. }
            40. -(NSData*)toJSON{
            41. NSError* error =nil;
            42. id result =[NSJSONSerialization dataWithJSONObject:self
            43. options:kNilOptions error:&error];
            44. if(error !=nil)returnnil;
            45. return result;
            46. }@end
            47. 使用列子
            48. NSDictionary* myInfo =[NSDictionary dictionaryWithContentsOfJSONURLString:@"http://www.yahoo.com/news.json"];
            49. NSDictionary* information =[NSDictionary dictionaryWithObjectsAndKeys:@"orange",@"apple",@"banana",@"fig",nil];
            50. NSData* json =[information toJSON];
            51. 判斷是否可json化
            52. BOOL isTurnableToJSON =[NSJSONSerialization isValidJSONObject: object]

            posted on 2012-06-02 23:46 Wangkeke 閱讀(11901) 評(píng)論(1)  編輯 收藏 引用 所屬分類: IOS

            評(píng)論

            # re: IOS5 JSON  回復(fù)  更多評(píng)論   

            rqwrqweewry
            2013-05-14 11:54 | berw
            久久久久久伊人高潮影院| 久久午夜无码鲁丝片| 91精品国产91久久久久久蜜臀| 午夜久久久久久禁播电影| 久久久久无码精品国产不卡| 久久91精品国产91| 99久久国产宗和精品1上映| 欧美牲交A欧牲交aⅴ久久| 国产成人久久精品区一区二区| 久久精品无码专区免费青青| 精品国产一区二区三区久久| 777午夜精品久久av蜜臀| 66精品综合久久久久久久| 久久国产乱子伦精品免费强| 久久高潮一级毛片免费| 无码精品久久久天天影视| 狠狠人妻久久久久久综合蜜桃| 开心久久婷婷综合中文字幕| 日韩乱码人妻无码中文字幕久久| 91精品国产综合久久香蕉| 99久久国产亚洲综合精品| 97久久精品人人澡人人爽| 久久亚洲国产精品成人AV秋霞| 精品久久一区二区三区| 18岁日韩内射颜射午夜久久成人| 99久久免费只有精品国产| 亚洲精品蜜桃久久久久久| 久久97久久97精品免视看| 99久久99久久久精品齐齐| 久久久久久久久久久久久久| 欧美国产精品久久高清| 久久福利青草精品资源站免费| 精品久久久久久国产| 久久99这里只有精品国产| 久久久久一本毛久久久| 热re99久久精品国产99热| 999久久久免费精品国产| 996久久国产精品线观看| 奇米影视7777久久精品| 色综合久久无码中文字幕| 亚洲综合精品香蕉久久网|