匿名者安逸
2021-07-21 20:58
采纳率: 100%
浏览 42
已结题

PHP Json数据解析问题

想要把这些数据打印下来,反正就是没成功

想要打印的结果:
xxx考试
满分750
得分565

xxx考试
满分850
得分565

{"code":0,"msg":"获取一名学生成绩趋势摘要信息成功","data":[{"examId":"1420702","name":"南平市2020-2021学年第二学期高一质量检测","type":2,"score":393.5,"time":1625414400000,"manfen":750,"classRank":-2,"level":11.78,"stage":"E","className":"12","teacherComment":2,"classRankType":5},{"examId":"1433279","name":"建瓯一中高一三科","type":2,"score":113,"time":1625414400000,"manfen":200,"classRank":-2,"level":55.93,"stage":"C","className":"12","teacherComment":2,"classRankType":5},{"examId":"1425560","name":"高一","type":5,"score":24,"time":1624896000000,"manfen":250,"classRank":-2,"level":100,"stage":"A","className":"12","teacherComment":2,"classRankType":5},{"examId":"1365292","name":"2020-2021(下)建瓯一中高一期中考试","type":1,"score":565.5,"time":1621353600000,"manfen":1050,"classRank":-2,"level":30.38,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1296366","name":"建瓯一中2020-2021下学年级第一次月考(高一)","type":3,"score":611.5,"time":1617638400000,"manfen":1050,"classRank":-2,"level":40.02,"stage":"C","className":"12","teacherComment":2,"classRankType":5},{"examId":"1187511","name":"南平市2020—2021学年第一学期高一质量检测","type":2,"score":637.5,"time":1611590400000,"manfen":1050,"classRank":-2,"level":39.8,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1157390","name":"2020-21学年第一学期金太阳卷高一联考","type":3,"score":657,"time":1608134400000,"manfen":1050,"classRank":-2,"level":39.42,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1141624","name":"建瓯一中2020-2021上学年高一期中考试","type":1,"score":554,"time":1605628800000,"manfen":1050,"classRank":-2,"level":11.23,"stage":"E","className":"12","teacherComment":2,"classRankType":5},{"examId":"1096981","name":"建瓯一中2020-2021学年上学期高一月考","type":3,"score":627.5,"time":1602691200000,"manfen":1050,"classRank":-2,"level":42.24,"stage":"C","className":"12","teacherComment":2,"classRankType":5}]}

  • 好问题 提建议
  • 收藏

5条回答 默认 最新

  • 一眼青苔 2021-07-26 13:57
    已采纳

    php json解析:

    json对象想要解析出来,需要用到 json_decode($html, true) 这样的写法,返回将会是一个数组。

    关于数组中提取部分字段和值问题:

    从一个数组中想要提出所有的name字段值,需要用到php方法 array_column()

    结果截图:

    img

    源码:

    <?php
    
    $str = '{"code":0,"msg":"获取一名学生成绩趋势摘要信息成功","data":[{"examId":"1420702","name":"南平市2020-2021学年第二学期高一质量检测","type":2,"score":393.5,"time":1625414400000,"manfen":750,"classRank":-2,"level":11.78,"stage":"E","className":"12","teacherComment":2,"classRankType":5},{"examId":"1433279","name":"建瓯一中高一三科","type":2,"score":113,"time":1625414400000,"manfen":200,"classRank":-2,"level":55.93,"stage":"C","className":"12","teacherComment":2,"classRankType":5},{"examId":"1425560","name":"高一","type":5,"score":24,"time":1624896000000,"manfen":250,"classRank":-2,"level":100,"stage":"A","className":"12","teacherComment":2,"classRankType":5},{"examId":"1365292","name":"2020-2021(下)建瓯一中高一期中考试","type":1,"score":565.5,"time":1621353600000,"manfen":1050,"classRank":-2,"level":30.38,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1296366","name":"建瓯一中2020-2021下学年级第一次月考(高一)","type":3,"score":611.5,"time":1617638400000,"manfen":1050,"classRank":-2,"level":40.02,"stage":"C","className":"12","teacherComment":2,"classRankType":5},{"examId":"1187511","name":"南平市2020—2021学年第一学期高一质量检测","type":2,"score":637.5,"time":1611590400000,"manfen":1050,"classRank":-2,"level":39.8,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1157390","name":"2020-21学年第一学期金太阳卷高一联考","type":3,"score":657,"time":1608134400000,"manfen":1050,"classRank":-2,"level":39.42,"stage":"D","className":"12","teacherComment":2,"classRankType":5},{"examId":"1141624","name":"建瓯一中2020-2021上学年高一期中考试","type":1,"score":554,"time":1605628800000,"manfen":1050,"classRank":-2,"level":11.23,"stage":"E","className":"12","teacherComment":2,"classRankType":5},{"examId":"1096981","name":"建瓯一中2020-2021学年上学期高一月考","type":3,"score":627.5,"time":1602691200000,"manfen":1050,"classRank":-2,"level":42.24,"stage":"C","className":"12","teacherComment":2,"classRankType":5}]}';
    
    $array = json_decode($str, true);
    
    $data = $array['data'];
    
    // 方法一
    foreach ($data as $k => $v) {
        echo $v['name'] . PHP_EOL;
        echo '满分' . $v['manfen'] . PHP_EOL;
        echo '得分' . $v['score'] . PHP_EOL;
    }
    
    //方法二
    $name = array_column($data,'name');         //将会提出所有的nama值
    $manfen = array_column($data,'manfen');     //将会提出所有满分的值
    $score = array_column($data,'score');       //将会提出所有的得分值
    
    
    已采纳该答案
    评论
    解决 1 无用
    打赏 举报
  • SinMu-L 2021-07-21 21:32

    如果你这个数据室前端传递过来的json数据的话,json_decode函数可以把字符串格式的json数据转换为数组

    评论
    解决 无用
    打赏 举报
  • mynamecxy 2021-07-22 00:39

    $data_arr = json_decode($data,true); //加true代表json解析成数组不使用表示json解析成对象(对象要用->获取数组使用[]获取
    //这里使用 echo 输出 pre标签后可以更直观
    print_r($data);//打印后你看完就知道怎么具体使用了

    评论
    解决 无用
    打赏 举报
  • 江天暮雪丨 2021-07-22 09:05

    json_decode($html, true); 加了true后,返回的结果是一个数组

    $obj = json_decode($html, true);
    $name = $obj['data'][0]['name'];
    
    评论
    解决 无用
    打赏 举报
  • 爱吃西红柿! 2021-07-23 11:54

    json_decode 转换成数组的格式

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题