2012-11-23 111 views
0

我在查询twitter api以获取hashtaggrails 2.1.1中的推文。在grails视图中渲染Json响应

我的控制器是

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 response如下:

{ 
     "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地图。

错误我得到的是:

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() 

我通过How do I access A JSON Object(String) from GSP page?去,但无法得到帮助。

Helpppp !!!

回答

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 
    } 
+0

Prayag嗨,你怎么解决groovx。 net.http依赖关系?我尝试了一些我在网络上找到的解决方案,但没有任何工作。 – Ray

+0

看一看[NepTweetsController.groovy](https://github.com/iPrayag/WhoTweetsNepal/blob/NepalTweets/grails-app/controllers/whotweetsnepal/NepTweetController.groovy)和[BuildConfig.groovy](https://开头github.com/iPrayag/WhoTweetsNepal/blob/NepalTweets/grails-app/conf/BuildConfig.groovy)。希望它可以帮助你。依赖应该是'编译( 'org.codehaus.groovy.modules.http助洗剂:HTTP的助洗剂:0.5.0'){ 排除 “共享记录”, “XML的API”, “常规” }' – prayagupd

4

你一定要

def tweetJson = JSON.parse(json); 

(这将返回一个JSONObject,这是一个Map),而不是

def tweetJson = new JSON(json) as Map; 

但它看起来像你有一个位名称冲突的在你的代码 - 推测是JSON中的

http.request(GET, JSON) 

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