gpt4 book ai didi

ios - 如何在 iOS App 中解析 JSON

转载 作者:可可西里 更新时间:2023-11-01 03:26:48 24 4
gpt4 key购买 nike

我从 Twitter 以字符串的形式收到回复,

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

这里是字符串的例子

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

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

@KristinaKlp saluditos y besos d colores!

那么,我怎样才能获取字符串并对其进行解析,这样我就有希望获得数组中的所有消息?

非常感谢!

最佳答案

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

SBJson 文档:

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

  • null -> NSNull
  • string -> NSString
  • array -> NSMutableArray
  • object -> NSMutableDictionary
  • true -> NSNumber's -numberWithBool:YES
  • false -> NSNumber's -numberWithBool:NO
  • integer up to 19 digits -> NSNumber's -numberWithLongLong:
  • all other numbers -> NSDecimalNumber

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.

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 to JSON

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

  • NSNull -> null
  • NSString -> string
  • NSArray -> array
  • NSDictionary -> object
  • NSNumber's -initWithBool:YES -> true
  • NSNumber's -initWithBool:NO -> false
  • NSNumber -> number

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

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:

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 和 Yet Another JSON Library

关于ios - 如何在 iOS App 中解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7530173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com