解析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解析的更多相关文章

  1. 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 ...

  2. iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson

    相关的第三方类库大家可以去github上下载 1.NSJSONSerialization 具体代码如下 : - (void)viewDidLoad { [super viewDidLoad]; NSD ...

  3. iOS - Json解析精度丢失处理(NSString, Double, Float)

    开发中处理处理价格金额问题, 后台经常返回float类型, 打印或转成NSString都会有精度丢失问题, 因此使用系统自带的NSDecimalNumber做处理, 能解决这问题:经过测试其实系统NS ...

  4. iOS json解析中包含“\n”等解析出错

    文题算是解决了,把特殊字符替换一下:-(NSString *)JSONString:(NSString *)aString {    NSMutableString *s = [NSMutableSt ...

  5. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  6. ios基础篇(二十七)—— Json解析

    一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...

  7. ios的网络数据下载和json解析

    ios的网络数据下载和json解析 简介 在本文中,笔者将要给大家介绍如何使用nsurlconnection 从网上下载数据,以及解析json数据格式,以及如何显示数据和图片的异步下载显示. 涉及的知 ...

  8. IOS中Json解析的四种方法

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...

  9. IOS数据解析JSON

    //非原创 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSO ...

随机推荐

  1. SLR,语法分析表的构建

    太累了,感觉不会再爱了.执行了跟编译原理上的一模一样的例子,输出了正确结果 #include <stdio.h> #include <malloc.h> #include &l ...

  2. Html5新标签及用法

    HTML 5 是一个新的网络标准,目标在于取代现有的 HTML 4.01, XHTML 1.0 and DOM Level 2  HTML 标准.它希望能够减少浏览器对于需要插件的丰富性网络应用服务( ...

  3. 【BUG】---ng-show/ng-hide修改成功但没有效果

    问题:点击搜索,隐藏tab,出现搜索结果列表,调试也成功修改了分别元素的ng-hide就是没有效果 我的错误: <div ng-hide="{{isShow}}" class ...

  4. Android 联系人字母排序(仿微信)

    现在很多APP只要涉及到联系人的界面,几乎都会采取字母排序以及导航的方式.作为程序猿,这种已经普及的需求还是需要学习的,于是小生开始了在网上默默的学习之路,网上学习的资料质量参差不齐,不过也有很不错的 ...

  5. POJ 2456

    #include <iostream> #include <vector> #include <algorithm> using namespace std; un ...

  6. UI1_应用的程序的生命周期

    // // AppDelegate.m // UI1_应用的程序的生命周期 // // Created by zhangxueming on 15/6/29. // Copyright (c) 201 ...

  7. python基础:三层循环

    三层循环基本演示: break_flag = False #标记1 break_flag2 = False #标记2 break_flag3 = False #标记3 while not break_ ...

  8. 【转载】#273 - Parameter Modifier Summary

    Here's a summary of the different parameter modifiers and how the behavior changes for each, when us ...

  9. eclipse优化(部分)

    1. 增强Eclipse(MyEclipse)输入代码提示功能 一般设置: (1). 打开Eclipse,选择打开" Window -- Preferences". (2). 在目 ...

  10. windows内存映射学习及帮助类实现

    本文通过创建文件内存映射类,学习windows内存映射相关知识:创建内存映射文件后,可以按照内存操作方式操作文件:支持32位程序处理超过4G大小的文件. 感谢http://blog.csdn.net/ ...