douping6871 2013-11-13 17:02
浏览 84
已采纳

如何在java中读取这个json数组

i have a json array generated by this php:

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

which returns:

[{"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]

well, i try to read that, but java only read until "false"... that array are 2 arrays which joined and printed, so i try this:

$row1=str_replace ( "false" , '{"salto":1}' , $row );
    $row1[]=false;
    print_r (json_encode($row1));

that returns the sames but "" instead "false" and a "false" in the end but java continues reading until now the "" i use that for read 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){}

so my question is how read this, or change some lines on php

  • 写回答

2条回答 默认 最新

  • dongqiaogouk86049 2013-11-13 17:19
    关注

    First of all in your PHP you shouldn't use mysql_* functions as they are deprecated and dangerous. Your code could be vulnerable to SQL Injection. Consider using PDO

    You could probably make this much faster by running a single query that would also get rid of the false values. You certainly don't need to stuff everything into an array and then loop over it.

    Here's my take:

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

    This should solve your problem with false values because they are likely due to the second query not being checked for errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样