您好即可。 我有一行json数据@ my webservice。所有Json数据均以此格式提供,仅提供URL和链接。所以它是我所知道的JsonObject。 Short说我请求,结果总是以url结尾。所以输出是:
{"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 - &gt; 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 - &gt; 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;
}
}
答案 0 :(得分:1)
你得到的是JSONObject而不是JSONArray。
{ // represetns json object node
"Url":"www.google.com"
}
SO改为
JSONObject jsonobject= jParser.getJSONFromUrl(url);
解析
String url = jsonobject.getString("Url");
编辑:
您需要更改此
jsonarr = new JSONArray(json);
要
JSONObject job = new JSONObject(json); // considering you get the above json
并返回
return job;
答案 1 :(得分:1)
您从响应中获取了一个Json对象。由于{"Url":"www.google.com"}
是JSONObject。
所以行
JSONArray json = jParser.getJSONFromUrl(url);
应该是
JSONObject json = jParser.getJSONFromUrl(url);
在阅读数据时,您只需要
String json_url = json.getString(TAG_Url);
而不是使用for循环。
查看更新的课程
public class JSONParser {
static InputStream is = null;
static String json = "";
JSONObject jsonObject = null; // Updated here
// konstruktor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
jsonObject = null; // Updated here
// 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 {
jsonObject = new JSONObject(json); // Updated here
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retunerer json object tilbage
return jsonObject; // Updated here
}
}
这适用于您当前的json。请参阅// Updated here
以了解我已更新的内容。
答案 2 :(得分:1)
您在JSONParser
课程中的错误,而您正在返回JSONArray
并且您正试图获得JSONObeject
。请参阅以下代码。
public class JSONParser {
static InputStream is = null;
static String json = "";
JSONObject jsonarr=null;
// konstruktor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {// Change return type from `JSONArray` to `JSONObject`
JSONObject 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 JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retunerer json object tilbage
return jsonarr;
}
}
然后使用以下代码。
JSONObject json = jParser.getJSONFromUrl(url);
和
String url = json.getString("url");