dongsuyou6938 2014-05-12 04:43
浏览 51
已采纳

json数据转换为php变量

I have to parse this json data using PHP and store the values into PHP variables.

{  
"sender": "am@email.com",  
"receiver": "ak@email.com",  
"msg_id": "msg1_am@email.com",   
"subject": "Group Discussion",   
"references": ["msg1_aman@email.com","msg1_s@email.com","msg1_v@email.com"]
}

I am using this PHP code, its not working; pls check it.

For the fields 'sender', 'receiver', 'msg_id'and 'subject, I am using PHP variables '$msg_id', '$sender', '$receiver' and '$subject'.
I am trying to store the data of 'references' in the array 'ref_id'.

Also, the file 'dataset_f.json' has content in the following way:

{ "sender":"aman@email.com","receiver":"akash@email.com","msg_id":"msg1_aman@email.com","subject": "Project Discussion","references":["msg1_aman@email.com","msg1_s@email.com","msg1_v@email.com"]}

{ "sender":"akash@email.com","receiver":"aman@email.com","msg_id":"msg1_akash@email.com","subject": "Project Discussion","references":["msg1_aman@email.com","msg1_s@email.com","msg1_v@email.com"]}

Thats why the php code is reading the file line by line

<?php
$h1 = fopen("dataset_f.json", "r");
while(!feof($h1)){
$line = fgets($h1);
$test_case = json_decode($line);

$ref_id = array();
$msg_id = $test_case->{'msg_id'};
$sender = $test_case->{'sender'};
$receiver = $test_case->{'receiver'};
$subject = $test_case->{'subject'};
foreach($test_case as $val)
 {
    foreach($val -> references as $refer)
    {
        array_push($ref_id, $refer->ref);
    }
 }

// printing the values
$arrlen=count($ref_id);
 for($x=0;$x<$arrlen;$x++)
 { echo $ref_id[$x]." "; }
echo $msg_id." ".$sender." ".$receiver." ".$subject." <br> ";

}
?>
  • 写回答

1条回答 默认 最新

  • dsagzmosl32217092 2014-05-12 05:14
    关注

    Store the whole file as a string, decode it and store the values in you variables. Show the json your using. your using the -> incorrect, thats for objects not arrays, json_decode() returns an associative array if the next param is true. ref_id will hold a string thats comma separated.

    dataset_f.json

    {
        "sender":"am@email.com",
        "receiver":"ak@email.com",
        "msg_id":"msg1_am@email.com",
        "subject":"Group Discussion",
        "references":["msg1_aman@email.com","msg1_s@email.com","msg1_v@email.com"]
    }
    

    php code

    $json = file_get_contents("dataset_f.json");
    
    $data = json_decode($json, true);
    
    // here depends on the json
    
    $ref_id = ( ( is_array( $data["references"] ) ? implode(", ", $data["references"]) : $data["references"] ) ); // array to string
    $msg_id = $data["msg_id"];
    $sender = $data["sender"];
    $receiver = $data["receiver"];
    $subject = $data["subject"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化