我有这个php生成的json数组:
$filtros=mysql_query("SELECT id_transporte FROM user_trans WHERE id_usuario = '".$id_usuario ."'");
for($i=0; $i<=mysql_num_rows($filtros); $i++){
$rs[]= mysql_fetch_array($filtros,MYSQL_ASSOC);
}
foreach($rs as $valor){
if (is_array($valor)) {
foreach($valor as $valor1){
$reportes[]= mysql_query("
SELECT * FROM transportes
WHERE id_transporte='".$valor1."'
ORDER BY fecha_reporte DESC ");
}
}
}
foreach($reportes as $valor){
if($valor != false){
for($i=0; $i<=mysql_num_rows($valor); $i++){
$row[]= mysql_fetch_array($valor,MYSQL_ASSOC);
}
}
}
print_r (json_encode($row));
返回:
[{"id_report":"73","id_transport":"624","txt_report":"Report0"},
{"id_report":"46","id_transport":"624","txt_report":"Report1"},false,
{"id_report":"74","id_transport":"9999","txt_report":"Report2"},
{"id_report":"52","id_transport":"9999","txt_report":"Report3"},false]
好吧,我尝试读取该内容,但是java只读取直到“ false” ...
该数组是连接并打印的2个数组,所以我尝试这样做:
$row1=str_replace ( "false" , '{"salto":1}' , $row );
$row1[]=false;
print_r (json_encode($row1));
返回相同的内容,但是最后用“”代替“ false”,最后是“ false”
但是Java会继续读取直到现在的“”
我用它来读取json
if (jdata!=null && jdata.length() > 0){
JSONObject json_data = null;
try{
System.out.println( jdata.length() );//java there print all the array length ignoring the "false" o ""
for (int i=0; i < jdata.length(); i++){
reportsId[i]=jdata.getJSONObject(i).getString("id_report");
transportId[i]=jdata.getJSONObject(i).getString("id_transport");
txt_report[i]=jdata.getJSONObject(i).getString("txt_report");
System.out.println( " i= "+ i);
}
}
}catch(Exception e3){}
所以我的问题是如何阅读此内容,或更改php上的某些行
参考方案
首先,在PHP中,您不应使用mysql_*
函数,因为它们已过时且危险。您的代码可能容易受到SQL注入的攻击。考虑使用PDO
您可以通过运行单个查询来消除错误值,从而使此过程更快。您当然不需要将所有内容填充到数组中然后在其上循环。
这是我的看法:
$resp = mysql_query("SELECT t.* FROM transportes t
JOIN user_trans ut ON t.id_transporte=ut.id_transporte
WHERE ut.id_usuario='$id_usuario'
ORDER BY fecha_reporte DESC");
if(!$resp) {
// handle the error!
die(mysql_error())
}
$output = [];
while( $row = mysql_fetch_assoc($resp) ) {
$output[] = $row;
}
print_r(json_encode($output));
这应该可以解决错误值的问题,因为错误值很可能是由于没有检查第二个查询是否存在错误。
JSON PATH字段NULL检查表达式 - java我有一个像bellow的json数组:{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sa…
Java-搜索字符串数组中的字符串 - java在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
Java Map,如何将UTF-8字符串正确放置到地图? - java我有一个地图,LinkedHashMap更确切地说。我想在上面放一个字符串对象。然后,我读取此值以查看实际存储的内容。字符串本身具有非ASCII字符(西里尔文,韩文等)。将其放到地图上然后阅读后,这些字符将替换为??? s。一些代码:Map obj = new LinkedHashMap(); System.out.println("name: &…
Java Double与BigDecimal - java我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…