java - JSON语法错误的php函数?

标签 java php android json

我在 Android 应用程序中解析 JSon 数据时遇到问题。我使用 PHP 脚本从 mysql 获取数据并将其放入 JSON 中。问题是我可以打印结果,并且我看到我的表中的所有项目。

JSON data from database

但是当数据传递到字符串生成器时,一切都会出错。解析 JSON 的数组后,我的数组中只有一个对象? 希望您能帮助我或给我一些建议。

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.1.10/getAllPeople.php");
    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
   }

//convert response to string
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");
       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }
//paring data
int id;
double Lat;
double Long;
String hulp = "";

v3 = (TextView) findViewById(R.id.tv3);
//v3.setText("Resultaat: " + result);

try{
      jArray = new JSONArray(result);
      for(int i=0;i<jArray.length();i++){                 
             JSONObject json_data = jArray.getJSONObject(i);
             id = json_data.getInt("id");
             Long = json_data.getDouble("long");
             Lat = json_data.getDouble("lat");
             hulp = hulp + "\n" +  "Long: " + Long + " Lat: " + Lat;
             mob.add(new MobieleFlitsers(id,Long,Lat));
         }

      for(int i=0;i<mob.size();i++){

          hulp = " " + mob.size();
        }
        v3.setText(hulp);
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "Geen locatie's gevonden" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }

我添加了 PHP 代码,因为它存储了错误的 JSON 格式。

        $q=mysql_query("SELECT * FROM people");

    //while($e=mysql_fetch_assoc($q))
    //$output[]=$e;
    //print(json_encode($output));
    //mysql_close();

    if (!$q) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
    }

    if (mysql_num_rows($q) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    while ($row = mysql_fetch_row($q)) {
        $output[]=$row;
        print(json_encode($output));


    }
    mysql_close();
}

}

?>

最佳答案

查看您的图像后,您的 JSON 似乎无效:

[{"id": 1, "long": 5000, "long": 5000}][{"id": 1, "long": 5000, "long": 5001}]

这应该是:

[{"id": 1, "long": 5000, "long": 5000}, {"id": 1, "long": 5000, "long": 5001}]

请注意,在您当前的代码中,每个 Object 都包含在方括号中,而应该只有一个 Array 包含所有 对象应该用逗号分隔。

尝试将 JSON 输出放入 JSONLint仔细检查其有效性。

关于java - JSON语法错误的php函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8502444/

相关文章:

java - JCheckBox 的值重置/不变

php - 数据库中插入记录成功,但web系统无法识别

php - 链接到另一个 Action

Android 图像缩放为什么不使用 ScrollView

java - 什么是 HibernateTemplate 类?

java - 包声明上面的 javadoc 注释有什么作用?

android - 从android帐户获取密码

android - 下载管理器下载文件

java - "Serializable"类中的字段应该是 transient 的或可序列化的

PHP 表单欺骗和验证