• <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(NSDictionary、NSData) 轉換為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
            欧美喷潮久久久XXXXx| 日本福利片国产午夜久久| 久久久久99精品成人片牛牛影视 | 久久综合香蕉国产蜜臀AV| 久久夜色精品国产噜噜噜亚洲AV| 久久精品www人人爽人人| 99精品国产在热久久| 久久久无码精品午夜| 亚洲中文字幕无码久久2020| 精品久久久无码人妻中文字幕豆芽| 国产精品VIDEOSSEX久久发布| 久久99国产精品久久99小说 | 人妻少妇久久中文字幕| 天天爽天天爽天天片a久久网| 性做久久久久久免费观看| .精品久久久麻豆国产精品 | 久久久国产99久久国产一| 久久精品天天中文字幕人妻| 久久WWW免费人成—看片| 国产精品9999久久久久| 亚洲国产成人精品91久久久| 狠狠色婷婷综合天天久久丁香| 亚洲日本va午夜中文字幕久久| 精品精品国产自在久久高清| 亚洲色大成网站www久久九| 理论片午午伦夜理片久久| 亚洲精品高清久久| 精品久久久久久亚洲| 精产国品久久一二三产区区别| 狠狠人妻久久久久久综合蜜桃| 久久国产色AV免费观看| 伊人久久大香线蕉亚洲五月天| 一级a性色生活片久久无少妇一级婬片免费放 | 日韩亚洲国产综合久久久| 91精品免费久久久久久久久| 久久精品aⅴ无码中文字字幕不卡| 久久国产欧美日韩精品免费| 女同久久| 无码日韩人妻精品久久蜜桃| 2020久久精品亚洲热综合一本| 最新久久免费视频|