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个回答

入库:转成数组格式后用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);

u011643463
原来我在古代 大神大神,十分感谢!最后json那个逗号是我忘删了,不好意思。最后提醒下自己,json_decode的第二个参数一定要给true
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐