zoukankan      html  css  js  c++  java
  • 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]



    Wangkeke 2012-06-02 23:46 发表评论
  • 相关阅读:
    【每日一题】16.Treepath (LCA + DP)
    M-SOLUTIONS Programming Contest 2020 游记 (AB水题,CD模拟,E题DFS)
    关于“Github上传以及Clone时发生的 Failed to connect to github.com port 443: Timed out 问题解法记录
    【离散数学】学习笔记目录
    【每日一题】15.Xorto (前缀和枚举)
    【动态规划】动态规划基础 (OI wiki)
    【每日一题】14.Accumulation Degree(树形DP + 二次扫描)
    AtCoder Beginner Contest 199 游记(AB水题,C字符串操作,D搜索,E状压)
    JXUST_NC
    LinkedList源码阅读笔记
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2537104.html
Copyright © 2011-2022 走看看