如何parsingiOS应用程序中的JSON
我得到一个stringforms的Twitter响应,
我需要的是将注释的部分发送到数组,
这里是一个string的例子
[{"geo":null,"coordinates":null,"retweeted":false,... "text":"@KristinaKlp saluditos y besos d colores!"},{"geo":null,"coordinates...
所以我真正需要的是“文本”后的post:“=
@KristinaKlp saluditos y besos d colores!
那么,我怎样才能把这个stringparsing出来,这样我就可以得到数组中所有的消息了?
非常感谢!
我没有在iOS应用程序中完成JSONparsing,但是应该可以使用像json-framework这样的库。 这个库将允许你轻松地parsingJSON并从字典/数组中生成JSON(这实际上是所有的JSON组成的)。
SBJson文档:
JSON以如下方式映射到Objective-Ctypes:
- null – > NSNull
- string – > NSString
- 数组 – > NSMutableArray
- object – > NSMutableDictionary
- true – > NSNumber的-numberWithBool:YES
- false – > NSNumber的-numberWithBool:NO
- 整数最多19位数字 – > NSNumber's -numberWithLongLong:
- 所有其他数字 – > NSDecimalNumber
由于Objective-C没有用于布尔值的专用类,因此它们将变成NSNumber实例。 但是,由于这些是用-initWithBool:方法初始化的,所以它们可以正确地返回到JSON。 换句话说,他们不会默默地变成0或1; 他们会再次被表示为“真实”和“虚假”。
作为一个优化整数长达19位数字(长签名长整数的最大长度)变成NSNumber实例,而复杂的变成NSDecimalNumber实例。 因此,我们可以避免任何精度的损失,因为JSON允许可笑的大数字。
@page objc2json Objective-C到JSON
Objective-Ctypes以如下方式映射到JSONtypes:
- NSNull – > null
- NSString – >string
- NSArray – >数组
- NSDictionary – >对象
- NSNumber的-initWithBool:YES – > true
- NSNumber的-initWithBool:NO – > false
- NSNumber – >数字
@note在JSON中,对象的键必须是string。 NSDictionary键不需要,但是试图将一个带有非string键的NSDictionary转换成JSON会抛出一个exception。
使用-numberWithBool:方法创build的NSNumber实例将转换为JSON布尔值“true”和“false”值,反之亦然。 其他任何NSNumber实例都将按照您所期望的方式转换为JSON数字。
教程
有没有教程? 是! 这些都是由第三方人员提供的所有教程:
适用于iPhone的JSON框架 – 由John Muchow三部分组成的Flickr教程。 JSON通过HTTP在iPhone上 – 通过Dan Grigsby。 AS3 to Cocoa touch :JSON by Andy Jacobs。
还有其他库可以检查出来,像TouchJSON,JSONKit,还有另外一个JSON库
NSJSONSerialization完成把你的JSON数据转换成NSDictionary或NSArray的可用数据结构的工作。 我推荐它,更因为它是Cocoa公共接口的一部分,它由Apple维护。
但是,如果要将JSON的内容映射到Objective-C对象,则必须将NSDictionary / NSArray中的每个属性映射到对象属性。 如果你的对象有很多属性,这可能会有点痛苦。
为了使这个过程自动化,我build议你在NSObject上使用Motis类(个人项目)来完成它,因此它非常轻便和灵活。 你可以阅读如何使用它在这个职位 。 但只是为了向您展示,您只需要定义一个字典,将JSON对象属性映射到NSObject子类中的Objective-C对象属性名称:
- (NSDictionary*)mjz_motisMapping { return @{@"json_attribute_key_1" : @"class_property_name_1", @"json_attribute_key_2" : @"class_property_name_2", ... @"json_attribute_key_N" : @"class_property_name_N", }; }
然后通过执行parsing:
- (void)parseTest { NSData *data = jsonData; // <-- YOUR JSON data // Converting JSON data into NSArray (your data sample is an array) NSError *error = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if (error) return; // <--- If error abort. // Iterating over raw objects and creating model instances NSMutableArray *parsedObjects = [NSMutableArray array]; for (NSDictionary *rawObject in jsonArray) { // Creating an instance of your class MyClass instance = [[MyClass alloc] init]; // Parsing and setting the values of the JSON object [instance mjz_setValuesForKeysWithDictionary:rawObject]; [parsedObjects addObject:instance]; } // "parseObjects" is an array with your parsed JSON. // Do whatever you want with it here. }
字典中属性的设置是通过KeyValueCoding(KVC)完成的,您可以在通过KVCvalidation设置之前validation每个属性。
我最近不得不这样做。 看了那里的各种选项后,我扔JSONKit到我的应用程序(我发现它在StackOverflow的JSON讨论)。 为什么? A)这非常非常简单。 我的意思是,它只有基本的parsing/发射function,你还需要什么? B)非常快。 没有开销 – 完成工作。
我应该注意到,我从来没有做过JSON – 只听说过这个词,甚至不知道如何拼写它。 我从一无所有,到一个工作的应用程序,大约一个小时。 您只需将一个类添加到您的应用程序(.h,.m)中,将其实例化,然后将parsing器调用到字典对象。 瞧。 如果它包含一个数组,则只需获取objectForKey,将其转换为NSArray。 比这更简单,而且速度非常快。
为了更好地比较不同库在iOS上JSONparsing的速度,请看The Ultimate Showdown 。
-(IBAction)btn_parse_webserivce_click:(id)sender { // Take Webservice URL in string. NSString *Webservice_url = self.txt_webservice_url.text; NSLog(@"URL %@",Webservice_url); // Create NSURL from string. NSURL *Final_Url = [NSURL URLWithString:Webservice_url]; // Get NSData from Final_Url NSData* data = [NSData dataWithContentsOfURL: Final_Url]; //parse out the json data NSError* error; // Use NSJSONSerialization class method. which converts NSData to Foundation object. NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; // Create Array NSArray* Response_array = [json objectForKey:@"loans"]; NSLog(@"Array: %@", Response_array); // Set Response_array to textview. self.txt_webservice_response.text = [NSString stringWithFormat:@"%@" ,Response_array]; }
那么NSJSONSerialization呢? 我一直在使用它来parsingJSON