• <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
            国产激情久久久久久熟女老人| 久久久久久精品免费免费自慰| 久久久久亚洲精品天堂| 久久伊人精品青青草原高清| 丁香五月综合久久激情| 亚洲国产成人久久综合碰| 久久久无码一区二区三区| 91精品国产91久久久久久青草| 亚洲人成电影网站久久| 亚洲精品无码久久久久去q | 国产无套内射久久久国产| 久久久这里只有精品加勒比| 国内精品伊人久久久久AV影院| 欧美久久天天综合香蕉伊| 精品无码久久久久久尤物| 久久精品中文字幕有码| 国产精品免费福利久久| 伊人久久精品影院| 久久午夜福利电影| 国产精品久久一区二区三区| 亚洲女久久久噜噜噜熟女| 四虎久久影院| 亚洲午夜精品久久久久久浪潮| 久久免费小视频| 国产精品久久久久久久久| 免费久久人人爽人人爽av| 青青草国产97免久久费观看| 久久亚洲综合色一区二区三区| 精品久久久久久国产潘金莲| 久久亚洲精品无码VA大香大香| 久久亚洲高清综合| 久久青青草原精品国产软件| 99久久国产热无码精品免费久久久久| 欧美黑人又粗又大久久久| 色综合久久久久无码专区| 久久人妻少妇嫩草AV蜜桃| 久久久久久久女国产乱让韩| 久久亚洲精品国产精品婷婷| 97视频久久久| 精品综合久久久久久888蜜芽| 久久精品国产亚洲av麻豆小说|