【问题标题】:Value [] at count of type org.json.JSONArray cannot be converted to doubleorg.json.JSONArray 类型计数中的值 [] 无法转换为双精度
【发布时间】:2017-11-13 10:45:51
【问题描述】:

我正在使用 achartengine 饼图从我的数据库服务器中获取值。我想显示学生在每个部分的平均分数。但我在我的 logcat 中收到错误声明:

W/System.err: org.json.JSONException: Value [] at count of type org.json.JSONArray cannot be converted to double

这是我的 java 代码的一部分:

public class c_marks extends AppCompatActivity {
    private GraphicalView mChart;
    private String[] code;
    double value1, value2, value3, value4, value5, value6;
    ProgressDialog progressDialog;
    JSONParser jsonParser = new JSONParser();
    String COUNT_URL_1 = "http://fypesystem.com/marks_test1.php";
    String COUNT_URL_2 = "http://fypesystem.com/marks_test2.php";
    String COUNT_URL_3 = "http://fypesystem.com/marks_test3.php";
    private static final String TAG_COUNT = "count";
    JSONObject json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c_marks);

        new Count().execute();
    }

    class Count extends AsyncTask<String,String ,String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args) {
            try {
                json = jsonParser.getJSONFromUrl(COUNT_URL_1);
                value1 = json.getDouble(TAG_COUNT);
                json = jsonParser.getJSONFromUrl(COUNT_URL_2);
                value2 = json.getDouble(TAG_COUNT);
                json = jsonParser.getJSONFromUrl(COUNT_URL_3);
                value3 = json.getDouble(TAG_COUNT);

            }catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String file_url) {
            progressDialog.dismiss();
            openChart();
        }
    }

这个问题可能是因为我的PHP编码,因为我相信有问题,但我不知道如何解决。

marks_test3.php

require("config1.php");

$query="SELECT totalmarks, COUNT(*) FROM marks WHERE totalmarks <=30";

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute();
}catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

$rows = $stmt->fetchAll();

if ($rows){
    $response["success"]=1;
    $response["message"]=" Available";
    $response["count"]= array();

        array_push($response["count"]);
        echo json_encode($response);

        }else {
        $response["success"] = 0;
        $response["message"] = "No Available!";
        die(json_encode($response));
}

?>

如果你能告诉我问题,我真的很感激。

【问题讨论】:

  • 为了清楚起见,您可以通过该php代码分享您拥有的json。

标签: php android json


【解决方案1】:

count 是一个 JSON 数组,但您试图将其作为浮点数引用。

您需要:

1。在 PHP 中创建 JSON 对象时将其设置为数字,并将值设置为找到的行数:

$response["count"] = count($rows);

在这种情况下,使用json.getInt() 可能比使用json.getDouble() 更正确,或者

2。获取count 作为JSONArray,然后检查它的大小:

json.getJSONArray("count").length()

【讨论】:

    【解决方案2】:

    这不是访问 JSON 数据的正确方法 可能是您的数据是 JSONArray 格式,您直接将其分配给 float 所以请使用以下方法

    try(){
    JSONArray jarray=new JSONArray(your_data_from_url);
    
    for(int i =0; i<jarray. length();i++){
    
    JSONObject jobject=jarray.getJSONObject(i);
     value1 = jobject.getDouble(TAG_COUNT);
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    

    【讨论】:

    • 另外两个网址怎么样?
    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多