ios - 使用 RubyMotion 解析 JSON
<p><p>我有一个 RubyMotion 类,它从 Twitter 的搜索 API 中读取数据以创建 <code>Tweet</code> 对象,如下所示。</p>
<p>当我运行它时,我得到了这个错误:</p>
<pre><code>(main)> 2012-11-08 17:01:32.634 Hello -: unrecognized selector sent to instance 0xea1f800
2012-11-08 17:01:32.685 Hello json.rb:20:in `parse:': NSInvalidArgumentException: -: 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 *** Terminating app due to uncaught exception 'RuntimeError', reason: 'json.rb:20:in `parse:': NSInvalidArgumentException: -: 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:'
</code></pre>
<p><strong>我应该如何解析 JSON?</strong></p>
<pre><code>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
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>有几个 CocoaPods 可以解决这个问题。特别是 JSONKit。</p>
<p>编辑:这不是气泡包装错误。这是一个解释 Twitter 响应的问题。我有一个使用bubblewrap 和JSONKit 的示例。我很快就会把它放在 GitHub 上。简短的总结,Twitter 返回一个数组数组。第一个元素是该组中的内容,第二个元素是实际数据。您想要迭代最外层的数组以查找第一个元素是结果的数组,然后使用第二个元素来导出您的响应数据。以下是两种替代实现:</p>
<pre><code>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 != 'results'
@feed = []
item.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(:object)
BW::HTTP.get(url_string) do |response|
parsed = response.body.to_str.objectFromJSONStringWithParseOptions JKParseOptionValidFlags, error: error_ptr
if parsed.nil?
error = error_ptr
puts error.userInfo
@timer.setValid false
else
parsed.each do |item|
next if item != 'results'
@feed = []
item.each do |tweet|
@feed << {:from => tweet['from_user'], :text => tweet['text']}
end
end
end
end
@feed
end
</code></pre>
<p> <a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html</a> </p></p>
<p style="font-size: 20px;">关于ios - 使用 RubyMotion 解析 JSON,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/13293979/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/13293979/
</a>
</p>
页:
[1]