我正在尝试检索其中包含双重类型值的JSON数组。但是它显示了这个错误: 类型java.lang.double无法转换为JSONOBJECT。 这是我的代码:
public void searchProfession(){
String myJson;
try {
//
// Log.i(getClass().getSimpleName(), "send task - start");
HttpParams httpParams = new BasicHttpParams();
//
HttpParams p = new BasicHttpParams();
// p.setParameter("name", pvo.getName());
p.setParameter("user", "1");
// Instantiate an HttpClient
HttpClient httpclient = new DefaultHttpClient(p);
String url = "http://abh.netai.net/abhfiles/searchProfession.php";
HttpPost httppost = new HttpPost(url);
// Instantiate a GET HTTP method
try {
Log.i(getClass().getSimpleName(), "send task - start");
//fffffffffffffffffffffffffff
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
// 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");
}
result = sb.toString();
myJson=result;
if(inputStream != null)inputStream.close();
//ffffffffffffffffffffffffffff
//
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,
responseHandler);
// Parse
JSONObject json = new JSONObject(myJson);
JSONArray jArray = json.getJSONArray("result");
ArrayList<HashMap<String, String>> mylist =
new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
String s = e.getString("id");
String fname = e.getString(First_Name);
JSONObject jObject = new JSONObject(s);
map.put("id",s);
map.put(First_Name,fname);
mylist.add(map);
Toast.makeText(MapsActivity.this, "your id is"+s, Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log.i(getClass().getSimpleName(), "send task - end");
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
我尝试过人们提供的多种解决方案,但是不起作用。而且我的json数组在记事本中也没问题。 json数组: { “结果”:[{ “ID”: “34.769669”, “如first_name”: “76”}]}
答案 0 :(得分:1)
,你做
String s = e.getString("id");
所以现在s = 34.769669。
JsonObject是一个键值Object。 你不能只是从“34.769669”创建一个json对象。
就像你在这里一样:
JSONObject jObject = new JSONObject(s);
就像在做
JSONObject jObject = new JSONObject("34.769669");
编辑: 你甚至没有使用这个对象
JSONObject jObject = new JSONObject(s);
为什么要创造呢?