• <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>

            厚積薄發,滴水穿石

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

            IOS5 JSON

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

            //Foundation(NSDictionaryNSData) 轉換為JSon格式的NSData 用來發送

            //+ dataWithJSONObject:options:error:

             

            //JSON格式的Data轉換為Foundation(NSDictionaryNSData)   用來解析

            //+ JSONObjectWithData:options:error:

            code:

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

            1. 解析json成dic對象
            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. 把對象生成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 閱讀(11902) 評論(1)  編輯 收藏 引用 所屬分類: IOS

            評論

            # re: IOS5 JSON  回復  更多評論   

            rqwrqweewry
            2013-05-14 11:54 | berw
            欧美综合天天夜夜久久| 国产亚洲婷婷香蕉久久精品| 久久免费视频一区| 久久久久人妻一区二区三区vr| 久久国产乱子精品免费女| 99精品久久精品| 久久久一本精品99久久精品88| 久久超碰97人人做人人爱| 久久综合中文字幕| 亚洲国产成人久久综合区| 久久精品国产精品亚洲下载 | 国产精品久久久久a影院| 色成年激情久久综合| 亚洲欧美久久久久9999 | 色悠久久久久久久综合网| 久久精品成人免费看| 欧美精品福利视频一区二区三区久久久精品 | 色综合久久天天综合| 亚洲av日韩精品久久久久久a| 亚洲AV伊人久久青青草原| 国产精品美女久久久m| 久久99精品国产麻豆| 亚洲欧美一区二区三区久久| 中文字幕无码久久久| 青青国产成人久久91网| 精品久久久久久久无码| 囯产精品久久久久久久久蜜桃| 99久久国产综合精品女同图片 | 久久精品国产亚洲av麻豆蜜芽| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 99久久婷婷国产综合亚洲| 亚洲综合精品香蕉久久网| 欧洲国产伦久久久久久久 | 久久久国产精品亚洲一区| 久久只有这精品99| 亚洲午夜无码久久久久小说| 色综合久久88色综合天天 | 欧美成a人片免费看久久| 久久一区二区免费播放| 无夜精品久久久久久| 亚洲国产成人乱码精品女人久久久不卡|