以下是我的代码:
public class JSON extends Activity {
TextView json;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String twitterTimeline = getTwitterTimeline();
try {
String tweets = "";
JSONObject jObj = new JSONObject(twitterTimeline);
JSONArray jsonArray = jObj.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int j = i+1;
tweets +="*** " + j + " ***\n";
tweets += "Date:" + jsonObject.getString("trends") + "\n";
tweets += "Post:" + jsonObject.getString("name") + "\n\n";
}
json= (TextView)findViewById(R.id.json);
json.setText(tweets);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String getTwitterTimeline() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://api.twitter.com/1/trends/23424848.json");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
//Couldn't obtain the data
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
我收到错误消息:
无效的Cookie标头:“set-cookie:guest_id = v1%3A136496691433468960; 域= .twitter.com;路径= /; Expires =星期五,03-Apr-2015 05:28:34 UTC“。 无法解析expires属性:星期五,03-Apr-2015 05:28:34 UTC org.json.JSONException:值 [{ “as_of”: “2013-04-03T05:28:34Z”, “趋势”:[{ “事件”:NULL, “查询”: “%22Finding +鲂%22”, “URL”:“HTTP: //twitter.com/search?q=%22Finding+Dory%22","promoted_content":null,"name":"Finding 多莉 “},{” 事件 “:空,” 查询 “:” %23MentionADislike”, “URL”: “http://twitter.com/search?q=%23MentionADislike”, “promoted_content”:空, “名” : “#MentionADislike”},{ “事件”:空, “查询”: “%23PSG”, “URL”: “http://twitter.com/search?q=%23PSG”, “promoted_content”:空, “名”: “#PSG”},{ “事件”:空, “查询”: “%23IPLOpeningCeremony”, “URL”: “http://twitter.com/search?q=%23IPLOpeningCeremony”, “promoted_content” :空, “名”: “#IPLOpeningCeremony”},{ “事件”:空, “查询”: “%23UCL”, “URL”: “http://twitter.com/search?q=%23UCL” “promoted_content”:空, “名”: “#UCL”},{ “事件”:空, “查询”: “%23GamesWePlay”, “URL”:“http://twitter.com/search?q=% 23GamesWePlay “ ”promoted_content“:空, ”名“: ”#GamesWePlay“},{ ”事件“:空, ”查询“: ”尤文“, ”URL“:” http://twitter.com/search?q =尤文 “ ”promoted_content“:空, ”名“: ”尤文“},{ ”事件“:空, ”查询“: ”巴尔德斯“, ”URL“:” http://twitter.com/search?q =巴尔德斯 “ ”promoted_content“:空, ”名“: ”巴尔德斯“},{ ”事件“:空, ”查询“: ”巴萨“, ”URL“:” http://twitter.com/search?q =巴萨”, “promoted_content”:空, “名”: “巴萨”},{ “事件”:空, “查询”: “拜仁”, “URL”: “http://twitter.com/search?q=Bayern”, “promoted_content”:空, “名”: “拜仁”}], “地点”:[{ “WOEID”:23424848, “名”: “印度”}], “created_at”: “2013-04-03T05:19:03Z”} ] 类型org.json.JSONArray无法转换为JSONObject org.json.JSON.typeMismatch(JSON.java:107)
任何人都可以指导我在哪里做错了吗?
这种JSON格式如何解析和显示
[
{
"trends":[
{
"name":"#PappuCII",
"url":"http:\/\/twitter.com\/search?q=%23PappuCII",
"promoted_content":null,
"query":"%23PappuCII",
"events":null
},
{
"name":"#ReplaceMovieNamesWithKamina",
"url":"http:\/\/twitter.com\/search?q=%23ReplaceMovieNamesWithKamina",
"promoted_content":null,
"query":"%23ReplaceMovieNamesWithKamina",
"events":null
},
PLZ帮助我,我现在感到困惑..
答案 0 :(得分:4)
与logcat结果一样:
JSONArray无法转换为JSONObject
因为您在当前json字符串而不是JSONArray
中将JSONObject
作为根元素。因此,您需要先将当前字符串转换为JSONArray,然后从中提取所有JSONObject。将代码编写为:
JSONArray jArray = new JSONArray (twitterTimeline); //<convert string to JSONArray
for (int i = 0; i < jArray .length(); i++) {
// get all JSONObject from jArray here..
}
您当前的json格式为:
[ //<<<< this is JSONArray
{ //<<<< this is JSONObject inside JSONArray
},
.....
]