原来我在古代
2018-11-19 03:55
采纳率: 100%
浏览 1.3k
已采纳

PHP解析Json时,如何解析保存了对象的数组,

我有如下Json字符串
$Json=[{"ClassName":"Class1501","StuNum":"20152154","StuName":"学生姓名1","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152230","StuName":"学生姓名2","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152420","StuName":"学生姓名3","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152426","StuName":"学生姓名4","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152093","StuName":"学生姓名5","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152133","StuName":"学生姓名6","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152282","StuName":"学生姓名7","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152451","StuName":"学生姓名8","ClassID":"1"},]

这个json我是在c#端用Litjson打包的,并传到php来处理

在PHP中创建类:

class ImportStudentData{
public $StuNum;
public $StuName;
public $ClassName;
public $ClassID;
}

如果我这样写
$Arr=array();
$Arr=json_decode($json,true);

解析出来的是一个数组,我能正确访问到数组的长度,但是不能访问数组里的元素(我希望这个数组的内容是ImportStudentData对象)

我的目的:
把这个Json字符串解析到PHP中,并且最后的结果是,把json中每个对象的内容保存到ImportStudentData对象中,并最后存到一个数组里。
$Arr2={ImportStudentData1,ImportStudentData2, ....}

最后,我可以通过
ImportStudentData1->StuNum
ImportStudentData1->StuName来得到Json中的内容

最终目的:
我是想把这些玩意,通过php存到数据库中

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 木泥石人 2018-11-19 04:42
    已采纳

    入库:转成数组格式后用foreach函数遍历数组
    foreach ($arr as $key => $value) {
    $sql = "insert into table(ClassName,StuNum,StuName,ClassID)values('{$value['ClassName']}','{$value['StuNum']}','{$value['StuName']}','{$value['ClassID']}')"
    }
    显示数据
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1501','20152154','学生姓名1','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1501','20152230','学生姓名2','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1501','20152420','学生姓名3','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1501','20152426','学生姓名4','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1502','20152093','学生姓名5','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1502','20152133','学生姓名6','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1502','20152282','学生姓名7','1')
    insert into table(ClassName,StuNum,StuName,ClassID)values('Class1502','20152451','学生姓名8','1')

    最后:你的json数据去掉最后的‘,’,才能转化成数组
    json数据:
    $str = '[{"ClassName":"Class1501","StuNum":"20152154","StuName":"学生姓名1","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152230","StuName":"学生姓名2","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152420","StuName":"学生姓名3","ClassID":"1"},{"ClassName":"Class1501","StuNum":"20152426","StuName":"学生姓名4","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152093","StuName":"学生姓名5","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152133","StuName":"学生姓名6","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152282","StuName":"学生姓名7","ClassID":"1"},{"ClassName":"Class1502","StuNum":"20152451","StuName":"学生姓名8","ClassID":"1"}]';
    $arr = json_decode($str,true);

    打赏 评论

相关推荐 更多相似问题