嗨。 我有一行json數據@我的web服務。所有Json數據均以此格式提供,僅限URL和鏈接。所以它是我知道的JsonObject。 Short說我要求,結果總是以url結尾。所以輸出是:問題JsonObject解析無法轉換爲jsonarray
{"Url":"www.google.com"}
這是我做了
JSONArray json = jParser.getJSONFromUrl(url);
try {
ListBasedList.clear();
//for each loop til JSON data
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
String json_url = c.getString(TAG_Url);
if(json_url.equals(0) && json_url.equals(""))
{
LinearLayout lin_footer = (LinearLayout) findViewById(R.id.footer_layoutMain);
lin_footer.setVisibility(View.GONE);
}
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_Url, json_url);
ListBasedList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser fejl", "fejl da man prøve og hente data fra server " + e.toString());
}
return null;
}
錯誤發生在這裏
logcat的告訴我:
的JSONObject不能轉換到jsonarray
那麼我怎麼能有鏈接而不是錯誤?
更新#1 - > logcat的完整的錯誤
10-01 13:34:45.685: E/JSON Parser(24256): Error parsing data org.json.JSONException: Value {"url":"www.google.com"} of type org.json.JSONObject cannot be converted to JSONArray
更新#2 - > JsonParser類
public class JSONParser {
static InputStream is = null;
static String json = "";
JSONArray jsonarr=null;
// konstruktor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
JSONArray jsonarr=null;
// HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// json array paser til string
try {
jsonarr = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retunerer json object tilbage
return jsonarr;
}
}
安置自己的JSON響應。 –
向我們展示您的json – Raghunandan
{「Url」:「www.google.com」} – Tirolel