Android 在org.json.json.typeMismatch(json.java:107)
下面是我的代码:Android 在org.json.json.typeMismatch(json.java:107),android,json,Android,Json,下面是我的代码: public class JSON extends Activity { TextView json; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String twitterTimeline = g
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();
}
}
请帮助我,我现在感到困惑。如logcat结果所示:
无法将JSONArray转换为JSONObject
因为您将JSONArray
作为当前json字符串中的根元素,而不是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..
}
JSONArray-jArray=新的JSONArray(twitterTimeline)// 与logcat结果一样:
无法将JSONArray转换为JSONObject
因为您将JSONArray
作为当前json字符串中的根元素,而不是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..
}
JSONArray-jArray=新的JSONArray(twitterTimeline)//先生可以详细地告诉我。实际上我是android新手。为什么会出现“org.json.JSONException:No value for name”错误???转换此url时“”@Nandu:如果仍然存在问题,那么添加新代码和logcat结果并询问以获得更多帮助代码是相同的,先生,我遵循您昨天回答的代码…我上面给出的url没有显示名称的值..实际上我想列出趋势名称..趋势是一个包含名称的数组,网址etc@Nandu:您需要更改代码当前为twitter返回的json字符串结构,而不是尝试使用以前的代码来解析任何其他json字符串ρцσѕρєK先生可以详细告诉我。实际上我是android新手。为什么会出现“org.json.JSONException:No value for name”错误???转换此url时“”@Nandu:如果仍然存在问题,那么添加新代码和logcat结果并询问以获得更多帮助代码是相同的,先生,我遵循您昨天回答的代码…我上面给出的url没有显示名称的值..实际上我想列出趋势名称..趋势是一个包含名称的数组,网址etc@Nandu:您需要更改为twitter返回的当前json字符串结构的代码,而不是尝试使用以前的代码解析任何其他json字符串
[ //<<<< this is JSONArray
{ //<<<< this is JSONObject inside JSONArray
},
.....
]