Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

arrays - How to parse JSON in iOS App

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

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

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

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 docs:

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.

Tutorials

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...