immi khan
immi khan

Reputation: 67

Value type of java.lng.double cannot be converted to JSONOBJECT

i am trying to retrieve JSON Array which has double type values in it.but it show me this error: type java.lang.double cannot be converted to JSONOBJECT. this is my code:

   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();




    }

i have tried multiple solutions provided by people but does not work.and also my json array is ok in notepad. json array: {"result":[{"id":"34.769669","first_name":"76"}]}

Upvotes: 1

Views: 1490

Answers (1)

Shahar
Shahar

Reputation: 3692

In your code, you do

String s = e.getString("id");

so now s = 34.769669

JsonObject is a key value Object. You can't create a json object just from "34.769669".

like you do here:

JSONObject jObject = new JSONObject(s);

It's like doing

JSONObject jObject = new JSONObject("34.769669");

EDIT: You are not even using this object

JSONObject jObject = new JSONObject(s);

so why creating it?

Upvotes: 1

Related Questions