我正在查询twitter api,在hashtag
中使用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 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 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()
我经历了How do I access A JSON Object(String) from GSP page?,但无法获得帮助。
Helpppp !!!
答案 0 :(得分:4)
你绝对想要
def tweetJson = JSON.parse(json);
(将返回JSONObject
,而不是<{p}} Map
def tweetJson = new JSON(json) as Map;
但看起来你的代码中有一些名字冲突 - 可能是
中的JSONhttp.request( GET, JSON )
旨在成为HTTPBuilder内容类型而非grails.converters.JSON
。如果您可以清除此名称冲突(即删除import static
并说出ContentType.JSON
),那么事情可能会更顺利。
答案 1 :(得分: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
}