如何在php中解析动态密钥值

时间:2017-09-03 11:32:39

标签: php json

如何在json中管理动态密钥

 [
      {
        "batch_id": "132",
        "benf_id": "ANP-JAI-1190",
        "NA3": "",
        "NA4": "",
        "status": "Y",
        "NA5": "",
        "datapoint_id": "105",
        "status_update_time": "28/8/2017 ",
        "user_id": "santoshk",
        "pop_id": "40",
        "measuring": "1000ltr",
        "weight": "100gm"
      },

      {
        "batch_id": "133",
        "benf_id": "ANP-JAI-1195",
        "NA3": "",
        "NA4": "",
        "status": "Y",
        "NA5": "",
        "datapoint_id": "106",
        "status_update_time": "28/8/2017 ",
        "user_id": "santoshk",
        "pop_id": "41",
        "water": "1000ltr",
        "medicine": "100gm"
      }

    ]

//我的php代码

public function benf_status_update($jdata)
{
    $count=0;
    $this->load->model('m_sf_user');
    while($count < count($jdata))
    {
        $batch_id = $jdata[$count]->batch_id;
        $benf_id = $jdata[$count]->benf_id;
        $m3=$jdata[$count]->NA3;
        $m4=$jdata[$count]->NA4;
        $status = $jdata[$count]->status;
        $m5=$jdata[$count]->NA5;
        $m1=$jdata[$count]->0;
        $m2=$jdata[$count]->0;
        $datapoint_id = $jdata[$count]->datapoint_id;
        $status_update_time = $jdata[$count]->status_update_time;
        $user_id =$jdata[$count]->user_id;
        $pop_id = $jdata[$count]->pop_id;
        $benf_id = substr($benf_id,8,strlen($benf_id));
        $qry = "UPDATE pop_b_m_points a JOIN m_pop_batch b ON 
                        b.pop_id=".$pop_id."
                        and b.batch_id=".$batch_id."
                        and  a.data_p_id=b.dpoint_id
                        and a.benf_id=".$benf_id."
                        and a.m_point_id=".$datapoint_id."
                        SET a.status='".$status."',
                         a.m1='".$m1."',
                         a.m2='".$m2."',
                         a.m3='".$m3."',
                         a.m4='".$m4."',
                         a.m5='".$m5."' ,                   
                        a.update_time= (SELECT (DATE_FORMAT(STR_TO_DATE('".$status_update_time."', '%d/%m/%Y' ), '%Y%m%d')))";

        $this->m_sf_user->jSONservices_u($qry);
        $count++;
    }
    $x=array('json_status'=>"success");
    echo json_encode($x);
}

2 个答案:

答案 0 :(得分:0)

您只需使用json_decode

即可
json_decode($string) to convert String into Array/Object (stdClass). 

示例:

$myRawJson = '{
  "batch_id": "133",
  "benf_id": "ANP-JAI-1195",
  "NA3": "",
  "NA4": "",
  "status": "Y",
  "NA5": "",
  "datapoint_id": "106",
  "status_update_time": "28/8/2017 ",
  "user_id": "santoshk",
  "pop_id": "41",
  "water": "1000ltr",
  "medicine": "100gm"
}';

$myAssoc  = json_decode($myRawJson);
$myObject = (object)(json_decode($myRawJson));

var_dump($myAssoc['medicine']); //100gm
var_dump($myObject->medicine); //100gm

答案 1 :(得分:-1)

如果您不确定要获得哪些列,但确实想要保存所有列。然后创建一个额外的列“数据”,通过json_encode()

保存完整的数据集

如果您可以确保某些字段始终存在,请在单独的列中使用它们以便按这些字段进行过滤和排序,还可以使用数据列添加完整的数据集。