0

我正在查询 twitter api 以获取带有hashtagin的推文grails 2.1.1

我的控制器是

import grails.converters.*
import org.codehaus.groovy.grails.web.json.*; // package containing JSONObject, JSONArray,...
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

    class NepTweetController {
        def index() {
              //def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
              def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
              def http = new HTTPBuilder(restEndpointUrl);

              // perform a GET request, expecting JSON response data
              http.request( GET, JSON ) {
                    //uri.path = '/search.json'
                    //uri.query = [ q: '%23nepal' ]
                                response.success = { resp, json ->
                                             // def tweetsResult = json.results;
                                             // List parsedList = JSON.parse(json) as List;
                                             // JSONObject tweetJson = JSON.parse(json);
                                             //def tweetJson = JSON.parse(json);
                                              def tweets = new JSON(json) as Map;

                                              render(view: "index", model: [message: "Request sent" , tweets: tweets]);
                                 }
              }//end of request
        }//end of method
    }

我成功地得到了json 响应,如下所示:

  {
      "completed_in": 0.017,
      "max_id": 271907240007581696,
      "max_id_str": "271907240007581696",
      "next_page": "?page=2&max_id=271907240007581696&q=%23nepal%2F",
      "page": 1,
      "query": "%23nepal%2F",
      "refresh_url": "?since_id=271907240007581696&q=%23nepal%2F",
      "results": [
        {
          "created_at": "Fri, 23 Nov 2012 09:25:12 +0000",
          "from_user": "iBshnu",
          "from_user_id": 25051623,
          "from_user_id_str": "25051623",
          "from_user_name": "Bishnu Bhattarai",
          "geo": null,
          "id": 271907240007581696,
          "id_str": "271907240007581696",
          "iso_language_code": "ne",
          "metadata": {
            "result_type": "recent"
          },
          "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2622309791\/42z33jnw1kak9ttyqkrf_normal.jpeg",
          "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2622309791\/42z33jnw1kak9ttyqkrf_normal.jpeg",
          "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
          "text": "RT @dipakbhattarai: \u0930\u093e\u0937\u094d\u091f\u094d\u0930\u092a\u0924\u093f \u0921\u093e. \u0930\u093e\u092e\u092c\u0930\u0923 \u092f\u093e\u0926\u0935\u0926\u094d\u0935\u093e\u0930\u093e \u092e\u0919\u094d\u0938\u093f\u0930 \u0967\u096a \u092d\u093f\u0924\u094d\u0930 \u0926\u0932\u0939\u0930\u0941\u0932\u093e\u0908 \u0930\u093e\u0937\u094d\u091f\u094d\u0930\u093f\u092f \u0938\u0939\u092e\u0924\u093f\u0915\u094b \u0938\u0930\u0915\u093e\u0930 \u092c\u0928\u093e\u0909\u0928 \u0906\u0935\u094d\u0939\u093e\u0928\u0964 #Nepal",
          "to_user": null,
          "to_user_id": 0,
          "to_user_id_str": "0",
          "to_user_name": null
        },
        {
          "created_at": "Fri, 23 Nov 2012 09:24:58 +0000",
          "from_user": "Phanindra07",
          "from_user_id": 63104333,
          "from_user_id_str": "63104333",
          "from_user_name": "Phanindra Dahal",
          "geo": null,
          "id": 271907179404095488,
          "id_str": "271907179404095488",
          "iso_language_code": "en",
          "metadata": {
            "result_type": "recent"
          },
          "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2417859753\/febstuo4p4zltimnpky0_normal.jpeg",
          "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2417859753\/febstuo4p4zltimnpky0_normal.jpeg",
          "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
          "text": "RT @DeepakAdk: Chef's assault on #Nepal #Maoist reflects grassroots anger, excellent piece by my #AFP colleague @FrankieTaggart http:\/\/t.co\/M47qJeoD",
          "to_user": null,
          "to_user_id": 0,
          "to_user_id_str": "0",
          "to_user_name": null
        }
[....more....]

/views/nepTweet/index.gsp的是

<meta name='layout' content='main'/>
<b>${message}</b>

<p>Tweets</p>
<g:each in="${tweets}" var="tweet">
     <p>${tweet.text}</p>
</g:each>

但是,我将 json 解析为 grails Map 时出错。

我得到的错误是:

Error 500: Internal Server Error
URI
/WhoTweetsNepal/nepTweet
Class
groovy.lang.MissingMethodException
Message
No signature of method: grails.converters.JSON.entrySet() is applicable for argument types: () values: [] Possible solutions: every()

我经历了如何从 GSP 页面访问 JSON 对象(字符串)?,但无法获得帮助。

帮顶!!!

4

2 回答 2

4

你肯定想要

def tweetJson = JSON.parse(json);

(这将返回 a JSONObject,即 a Map)而不是

def tweetJson = new JSON(json) as Map;

但看起来您的代码中有一些名称冲突 - 大概是 JSON

http.request( GET, JSON )

旨在成为 HTTPBuilder 内容类型,而不是grails.converters.JSON. 如果您可以清除此名称冲突(即删除import static和说ContentType.JSON),那么事情可能会更顺利。

于 2012-11-23T13:42:08.633 回答
1

根据Ian Roberts的中肯建议,我得到了最终控制器的解决方案(意识到不需要解析它):

import grails.converters.*
import org.codehaus.groovy.grails.web.json.*; // package containing JSONObject, JSONArray,...
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*


    class NepTweetController {
        def index() {
              def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
              def http = new HTTPBuilder(restEndpointUrl);

              // perform a GET request, expecting JSON response data
              http.request( GET, ContentType.JSON ) {
                    //uri.path = '/search.json'
                    //uri.query = [ q: '%23nepal' ]
                                response.success = { resp, json ->
                                             def tweetsResult = json.results;

                                              render(view: "index", model: [message: "Request sent" , tweets: tweetsResult]);
                                 }
              }//end of request
        }//end of method
    }
于 2012-11-23T16:12:50.507 回答