如何在 iOS App 中解析 JSON [英] How to parse JSON in iOS App

查看:29
本文介绍了如何在 iOS App 中解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自 twitter 的字符串形式的回复,

Im getting a response from twitter in the form of a string,

我需要的是将评论的部分发送到数组,

What I need is to send the parts where is a comment to an array,

这里是一个字符串的例子

here an example of the string

[{"geo":null,"coordinates":null,"retweeted":false,... 
"text":"@KristinaKlp saluditos y besos d colores!"},{"geo":null,"coordinates...

所以我真正需要的是text"之后的帖子:"=

so what I really need are the posts after "text":" =

@KristinaKlpsaluditos y besos d colores!

@KristinaKlp saluditos y besos d colores!

那么,我怎样才能获取字符串并解析它,以便我有希望地获得数组中的所有消息?

So, how can I take the string and parse it so I get all the messages in an array hopefully?

非常感谢!

推荐答案

我还没有在 iOS 应用程序中对自己进行 JSON 解析,但是您应该能够使用像 json-framework.该库将允许您轻松解析 JSON 并从字典/数组中生成 json(这实际上是所有 JSON 的组成部分).

I haven't done JSON parsing myself in an iOS App, but you should be able to use a library like the json-framework. This library will allow you to easily parse JSON and generate json from dictionaries / arrays (that's really all JSON is composed of).

SBJson 文档:

JSON 通过以下方式映射到 Objective-C 类型:

JSON is mapped to Objective-C types in the following way:

  • null -> NSNull
  • string -> NSString
  • 数组 -> NSMutableArray
  • object -> NSMutableDictionary
  • true -> NSNumber 的 -numberWithBool:YES
  • false -> NSNumber 的 -numberWithBool:NO
  • 最多 19 位的整数 -> NSNumber 的 -numberWithLongLong:
  • 所有其他数字 -> NSDecimalNumber

由于 Objective-C 没有专门的布尔值类,这些变成 NSNumber 实例.然而,由于这些使用 -initWithBool: 方法进行初始化,它们返回 JSON适当地.换句话说,它们不会默默地突然变成0或1;它们将再次表示为真"和假".

Since Objective-C doesn't have a dedicated class for boolean values, these turns into NSNumber instances. However, since these are initialised with the -initWithBool: method they round-trip back to JSON properly. In other words, they won't silently suddenly become 0 or 1; they'll be represented as 'true' and 'false' again.

作为优化整数长度可达 19 位(最大长度对于有符号长整数)变成 NSNumber 实例,而复杂的变成 NSDecimalNumber 实例.因此,我们可以避免任何精度损失,因为 JSON 允许大到可笑的数字.

As an optimisation integers up to 19 digits in length (the max length for signed long long integers) turn into NSNumber instances, while complex ones turn into NSDecimalNumber instances. We can thus avoid any loss of precision as JSON allows ridiculously large numbers.

@page objc2json Objective-C 转 JSON

@page objc2json Objective-C to JSON

Objective-C 类型通过以下方式映射到 JSON 类型:

Objective-C types are mapped to JSON types in the following way:

  • NSNull -> null
  • NSString -> 字符串
  • NSArray -> 数组
  • NSDictionary -> 对象
  • NSNumber 的 -initWithBool:YES -> true
  • NSNumber 的 -initWithBool:NO -> false
  • NSNumber -> 号码

@note 在 JSON 中,对象的键必须是字符串.NSD字典键不必是,但尝试将 NSDictionary 转换为JSON 中的非字符串键将引发异常.

@note In JSON the keys of an object must be strings. NSDictionary keys need not be, but attempting to convert an NSDictionary with non-string keys into JSON will throw an exception.

使用 -numberWithBool: 方法创建的 NSNumber 实例是转换为 JSON 布尔值true"和false",反之亦然反之.任何其他 NSNumber 实例都被转换为一个 JSON 数字你所期望的方式.

NSNumber instances created with the -numberWithBool: method are converted into the JSON boolean "true" and "false" values, and vice versa. Any other NSNumber instances are converted to a JSON number the way you would expect.

教程

有教程吗?是的!这些都是提供的教程第三方人员:

Are there any tutorials? Yes! These are all tutorials provided by third-party people:

iPhone 的 JSON 框架 - John 的 Flickr 教程,分为三部分慕晓.iPhone 上的 HTTP 上的 JSON - 作者:Dan Grigsby.AS3 到 Cocoa touch:Andy Jacobs 的 JSON.

JSON Framework for iPhone - a Flickr tutorial in three parts by John Muchow. JSON Over HTTP On The iPhone - by Dan Grigsby. AS3 to Cocoa touch: JSON by Andy Jacobs.

您还可以查看其他库,例如 TouchJSON、JSONKit 和另一个 JSON 库

There are other libraries you can check out as well like TouchJSON, JSONKit, Yet Another JSON Library

这篇关于如何在 iOS App 中解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆