我有一个 RubyMotion 类,它从 Twitter 的搜索 API 中读取数据以创建 Tweet 对象,如下所示。
当我运行它时,我得到了这个错误:
(main)> 2012-11-08 17:01:32.634 Hello[39940:c07] -[__NSCFString bytes]: unrecognized selector sent to instance 0xea1f800
2012-11-08 17:01:32.685 Hello[39940:c07] json.rb:20:in `parse:': NSInvalidArgumentException: -[__NSCFString bytes]: unrecognized selector sent to instance 0xea1f800 (RuntimeError)
from tweets_controller.rb:11:in `create_tweets'
from tweets_controller.rb:7:in `tweets'
from twitter_view_controller.rb:21:in `tableView:numberOfRowsInSection:'
from app_delegate.rb:10:in `application:didFinishLaunchingWithOptions:'
2012-11-08 17:01:32.686 Hello[39940:c07] *** Terminating app due to uncaught exception 'RuntimeError', reason: 'json.rb:20:in `parse:': NSInvalidArgumentException: -[__NSCFString bytes]: unrecognized selector sent to instance 0xea1f800 (RuntimeError)
from tweets_controller.rb:11:in `create_tweets'
from tweets_controller.rb:7:in `tweets'
from twitter_view_controller.rb:21:in `tableView:numberOfRowsInSection:'
from app_delegate.rb:10:in `application:didFinishLaunchingWithOptions:'
我应该如何解析 JSON?
class TweetsController
def initialize
@twitter_accounts = %w(dhh google)
end
def tweets
@tweets ||= create_tweets
end
def create_tweets
BW::JSON.parse(twitter_search_results)["results"].each do |result|
@tweets << Tweet.new(result)
end
@tweets
end
def twitter_search_results
query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
url_string = "http://search.twitter.com/search.json?q=#{query}"
url_string_escaped = url_string.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
url = NSURL.URLWithString(url_string_escaped)
request = NSURLRequest.requestWithURL(url)
response = nil
error = nil
data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: error)
##raise "BOOM!" unless (data.length > 0 && error.nil?)
json = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
end
end
class Tweet
attr_reader :created_at, :from_user, :text
def initialize(tweet_result)
@created_at = tweet_result["created_at"]
@from_user = tweet_result["from_user"]
@text = tweet_result["text"]
end
end
Best Answer-推荐答案 strong>
有几个 CocoaPods 可以解决这个问题。特别是 JSONKit。
编辑:这不是气泡包装错误。这是一个解释 Twitter 响应的问题。我有一个使用bubblewrap 和JSONKit 的示例。我很快就会把它放在 GitHub 上。简短的总结,Twitter 返回一个数组数组。第一个元素是该组中的内容,第二个元素是实际数据。您想要迭代最外层的数组以查找第一个元素是结果的数组,然后使用第二个元素来导出您的响应数据。以下是两种替代实现:
def create_tweets_using_bubblewrap
@twitter_accounts = %w(dhh google)
query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
url_string = "http://search.twitter.com/search.json?q=#{query}"
BW::HTTP.get(url_string) do |response|
parsed = BW::JSON.parse response.body.to_str
parsed.each do |item|
next if item[0] != 'results'
@feed = []
item[1].each do |tweet|
@feed << {:from => tweet['from_user'], :text => tweet['text']}
end
end
end
@feed
end
def create_tweets_using_jsonkit
@twitter_accounts = %w(dhh google)
query = @twitter_accounts.map{ |account| "from:#{account}" }.join(" OR ")
url_string = "http://search.twitter.com/search.json?q=#{query}"
error_ptr = Pointer.new(bject)
BW::HTTP.get(url_string) do |response|
parsed = response.body.to_str.objectFromJSONStringWithParseOptions JKParseOptionValidFlags, error: error_ptr
if parsed.nil?
error = error_ptr[0]
puts error.userInfo[NSLocalizedDescriptionKey]
@timer.setValid false
else
parsed.each do |item|
next if item[0] != 'results'
@feed = []
item[1].each do |tweet|
@feed << {:from => tweet['from_user'], :text => tweet['text']}
end
end
end
end
@feed
end
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
关于ios - 使用 RubyMotion 解析 JSON,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/13293979/
|