【问题标题】:Cannot convert java string to JSON Array in Android无法在 Android 中将 java 字符串转换为 JSON 数组
【发布时间】:2020-05-26 18:56:37
【问题描述】:

下面是从 MySql 数据库中获取数据的查询,但问题是在浏览器中它成功输出了数据,但在 Android 应用程序中它给出了下面的错误

Java.Long.String Cannot Convert to JsonArray

if($stem=$con->prepare("select question from MCQs  where c_id=$c_id ")){
$stem->execute ();
$stem->bind_result($question);
$budget=array();
while($stem->fetch())
{
    $temp=array();

    $temp['question']=$question;

    array_push($budget,$temp);
}
echo json_encode($budget);

以下是 JSON 的 Android 端 Java 代码

                    JSONObject jsonObject = new JSONObject(response);
                    String success = jsonObject.getString("success");
                    JSONArray jsonArray = jsonObject.getJSONArray("data");

                    if (success.equals("1")) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            String questions = jsonObject1.getString("question").trim();
                            final String opt0ne = jsonObject1.getString("option1").trim();
                            ;
                            String opttwo = jsonObject1.getString("option2").trim();
                            ;
                            String optthree = jsonObject1.getString("option3").trim();
                            final String correctt = jsonObject1.getString("correct").trim();

【问题讨论】:

  • 我喜欢一个好的 Jason 数组。
  • “但在 Android 应用程序中它给出了以下错误” - 在哪里?我没有看到任何错误。

标签: java php android arrays json


【解决方案1】:
        JSONObject json = new JSONObject(result);
        JSONArray jArray = json.getJSONArray("data");
        for (int i=0;jArray.length(); i++)
        {
         JSONObject jsonObject1 = jsonArray.getJSONObject(i);

         //try this
         String msg= jsonObject1.getString("success"); 
       }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多