• <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, 評論 - 12, 引用 - 0
            數(shù)據(jù)加載中……

            IOS5 JSON

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

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

            //+ dataWithJSONObject:options:error:

             

            //JSON格式的Data轉(zhuǎn)換為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 閱讀(11903) 評論(1)  編輯 收藏 引用 所屬分類: IOS

            評論

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

            rqwrqweewry
            2013-05-14 11:54 | berw
            久久艹国产| 国内精品伊人久久久久av一坑| 久久久久国产一级毛片高清板| 午夜精品久久久久成人| 日日噜噜夜夜狠狠久久丁香五月 | AAA级久久久精品无码片| 天天久久狠狠色综合| 亚洲精品乱码久久久久久中文字幕 | 久久久久人妻精品一区三寸蜜桃| 伊人色综合九久久天天蜜桃| 国内精品久久久久影院免费| 久久久久人妻一区二区三区| 久久久久亚洲av毛片大 | 99久久久精品| 18岁日韩内射颜射午夜久久成人| 久久精品成人影院| 久久国产精品国产自线拍免费| 久久久久久久女国产乱让韩| 久久精品无码一区二区app| 国产精品一久久香蕉国产线看| 四虎影视久久久免费| 久久久久久久国产免费看| 国产成人久久激情91| 无码日韩人妻精品久久蜜桃| 亚洲七七久久精品中文国产| 久久国产成人午夜AV影院| 欧美日韩中文字幕久久伊人| 久久精品天天中文字幕人妻| 久久久久亚洲AV无码麻豆| 色欲综合久久中文字幕网| 亚洲精品无码久久久久久| 久久久久亚洲AV成人网人人网站 | 亚洲精品tv久久久久| 久久这里都是精品| 影音先锋女人AV鲁色资源网久久| 要久久爱在线免费观看| 亚洲国产精品嫩草影院久久| 久久亚洲sm情趣捆绑调教| 久久综合亚洲欧美成人| 狠色狠色狠狠色综合久久| 精品久久综合1区2区3区激情 |