如何在php中转换数组中的json

时间:2019-11-12 02:24:49

标签: php json

我刚学过php,请帮忙 我有一个json 我不知道这个json是否有效,那我如何启用json到数组

(select locn_brcd from locn_hdr where locn_id 
    in (select locn_id from 
        (select locn_id from at_wh_ghc1.at_case_hdr a 
            where a.case_nbr ) where rownum <= 1) -- a.case_nbr is alone in where clause??? 
                 -- It must be a.case_nbr = something or you need to remove the entire where clause itself
 and rownum = 1) "3rd",
ACTL_QTY,

如何将json转换为数组

这是我的代码

"[{id_service\":\"3\",\"reference_number\":\"\",\"tracking_number\":\"RJC-0000-0001\",\"kd_inbound\":\"INB-1000-0001\",\"tgl_inbound\":\"2019-11-08 00:00:00\",\"status_inb\":\"1\"},{\"id_service\":\"3\",\"reference_number\":\"\",\"kd_outbag\":\"BAG-1468-0001\",\"tanggal_outbag\":\"2019-11-08 00:00:00\",\"status_outbag\":\"1\"},{\"id_service\":\"3\",\"reference_number\":\"\",\"kd_outbound\":\"OTB-1826-0001\",\"tgl_outbound\":\"2019-11-08 14:04:00\",\"status_otb\":\"1\"},{\"id_service\":\"3\",\"reference_number\":\"\",\"tracking_number\":\"RJC-0000-0001\",\"kd_indes\":\"INB-DES-56730001\",\"tgl_indes\":\"2019-11-08 14:07:30\",\"status_indes\":\"1\"},{\"id_service\":\"3\",\"reference_number\":\"\",\"tracking_number\":\"RJC-0000-0001\",\"tgl_status\":\"2019-11-07 17:06:43\",\"status\":\"Consignee Unknown\"},{\"id_service\":\"3\",\"reference_number\":\"\",\"tracking_number\":\"RJC-0000-0001\",\"tgl_status\":\"2019-11-08 10:29:07\",\"status\":\"Closed\"}]"

如何正确替换json,这样输出

public function awb_get() {
    $id = $this->get('tracking_number');
    $arr= array(      
      $this->M_tarif->tampil_status_inbound($id),
      $this->M_tarif->tampil_status_otboundbag($id),
      $this->M_tarif->tampil_status_otboundori($id),
      $this->M_tarif->tampil_status_indes($id)
  );


    $result = str_replace(array('[',']','\n'), '',htmlspecialchars(json_encode($arr), ENT_NOQUOTES));
    $str = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $result);
    $json = '[';
    $json .= substr($str, 2,-1); 
    $json .= ']';
    $jsonData = preg_replace("/,(?!.*,)/", "", $json);
    $this->response($jsonData, 200);
}

2 个答案:

答案 0 :(得分:0)

这只是JSON编码为JSON字符串。所以只需decode两次。

<input ...><label>...</label>

答案 1 :(得分:-1)

要检查JSON是否有效:

function isJson($string) {
   json_decode($string);
   return (json_last_error() == JSON_ERROR_NONE);
}

然后将JSON解析为array

json_decode($string, true);

更新:

要从您的字符串中删除\,请使用:

stripslashes($string);