iOS JSON解析
解析json成dic对象
- -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error;
- NSDictionary* json =[NSJSONSerialization
- JSONObjectWithData:responseData //1
- options:kNilOptions
- error:&error];
- NSArray* latestLoans =[json objectForKey:@"loans"]; //2
- NSLog(@"loans: %@", latestLoans); //3
- }
- 把对象生成json string
- //build an info object and convert to json
- NSDictionary* info =[NSDictionary dictionaryWithObjectsAndKeys:[loan objectForKey:@"name"],
- @"who",
- [(NSDictionary*)[loan objectForKey:@"location"]
- objectForKey:@"country"],
- @"where",
- [NSNumber numberWithFloat: outstandingAmount],
- @"what",
- nil];
- //convert object to data
- NSData* jsonData =[NSJSONSerialization dataWithJSONObject:info
- options:NSJSONWritingPrettyPrinted error:&error];
- //print out the data contents
- jsonSummary.text =[[NSString alloc] initWithData:jsonData
- encoding:NSUTF8StringEncoding];
- 添加json方法至dic
- @interfaceNSDictionary(JSONCategories)
- +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
- -(NSData*)toJSON;
- @end
- @implementationNSDictionary(JSONCategories)
- +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress{
- NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
- __autoreleasing NSError* error =nil;
- id result =[NSJSONSerialization JSONObjectWithData:data
- options:kNilOptions error:&error];
- if(error !=nil)returnnil;
- return result;
- }
- -(NSData*)toJSON{
- NSError* error =nil;
- id result =[NSJSONSerialization dataWithJSONObject:self
- options:kNilOptions error:&error];
- if(error !=nil)returnnil;
- return result;
- }@end
- 使用列子
- NSDictionary* myInfo =[NSDictionary dictionaryWithContentsOfJSONURLString:@"http://www.yahoo.com/news.json"];
- NSDictionary* information =[NSDictionary dictionaryWithObjectsAndKeys:@"orange",@"apple",@"banana",@"fig",nil];
- NSData* json =[information toJSON];
- 判断是否可json化
- BOOL isTurnableToJSON =[NSJSONSerialization isValidJSONObject: object]
iOS JSON解析的更多相关文章
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson
相关的第三方类库大家可以去github上下载 1.NSJSONSerialization 具体代码如下 : - (void)viewDidLoad { [super viewDidLoad]; NSD ...
- iOS - Json解析精度丢失处理(NSString, Double, Float)
开发中处理处理价格金额问题, 后台经常返回float类型, 打印或转成NSString都会有精度丢失问题, 因此使用系统自带的NSDecimalNumber做处理, 能解决这问题:经过测试其实系统NS ...
- iOS json解析中包含“\n”等解析出错
文题算是解决了,把特殊字符替换一下:-(NSString *)JSONString:(NSString *)aString { NSMutableString *s = [NSMutableSt ...
- iOS - JSON 数据解析
iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...
- ios基础篇(二十七)—— Json解析
一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...
- ios的网络数据下载和json解析
ios的网络数据下载和json解析 简介 在本文中,笔者将要给大家介绍如何使用nsurlconnection 从网上下载数据,以及解析json数据格式,以及如何显示数据和图片的异步下载显示. 涉及的知 ...
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- IOS数据解析JSON
//非原创 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSO ...
随机推荐
- [转]div 让文字或图片居中
本文转自:http://qsfwy.iteye.com/blog/250206 在div 中让 文字或图片居中,请参考以下代码1: - - - - - - - - - - - - - - - - - ...
- 51nod 平均数(二分+树状数组)
题目链接: 平均数 基准时间限制:4 秒 空间限制:131072 KB 分值: 80 LYK有一个长度为n的序列a. 他最近在研究平均数. 他甚至想知道所有区间的平均数,但是区间数目实在太多了. 为了 ...
- JS辨别浏览器系统IOS或安卓
详细内容请点击 /* * 智能机浏览器版本信息: * */ (function($,window,document){ $.extend({ browser:{ ...
- hexo搭建静态博客
1. 环境环境 1.1 安装Git 请参考[1] 1.2 安装node.js 下载:http://nodejs.org/download/ 可以下载 node-v0.10.33-x64.msi 安装时 ...
- Javascript加载速度慢解决办法
通常我们的网站里面会加载一些js代码,统计啊,google广告啊,百度同盟啊,阿里妈妈广告代码啊,一堆,最后弄得页面加载速度很慢,很慢.解决办法:换一个js包含的方式,让javascript加载速度倍 ...
- js数组与字符串的相互转换方法
一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ...
- 在调用“Fill”前,SelectCommand 属性尚未初始化
在调用“Fill”前,SelectCommand 属性尚未初始化 是因为少写了一行代码: private readonly string strConnection = System.Configur ...
- [老老实实学WCF] 第八篇 实例化
老老实实学WCF 第八篇 实例化 通过上一篇的学习,我们简单地了解了会话,我们知道服务端和客户端之间可以建立会话连接,也可以建立非会话连接,通信的绑定和服务协定的 ServiceContract 的S ...
- XtraGrid使用心得(折叠式主细档、分组统计)
XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...
- JVM基础知识总结
因为没深入搞底层研究,所以也就没做很细致的笔记.相关笔记内容是直接从度娘那儿来的,重新删减.整理和加了点自己的东西. 1.JVM(Java Virtual Machine)是什么:JVM是一种用于计算 ...