本文介绍了如何写一个json文件作为PHP中的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些这样的数据
"name": "abc",
"adr": "bcd",
"partners": {
"101": {
"name": "xyz.com",
"prices": {
"1001": {
"description": "Single Room",
"amount": 125,
"from": "2012-10-12",
"to": "2012-10-13"
},
"1002": {
"description": "Double Room",
"amount": 139,
"from": "2012-10-12",
"to": "2012-10-13"
}
}
现在,我必须使用所有这些数据编写一个json并将其用作数据源.
Now, I have to write a json with all this data and use it as a data source.
我该怎么办?
推荐答案
您发布的数据不是有效的JSON.它错过了一些包围和结尾的括号.
The data you posted is not valid JSON. It misses some surrounding and ending brackets.
好,让我们修复一下...并将其另存为data.json
:
Ok, let's fix that... and save it as data.json
:
{
"name": "abc",
"adr": "bcd",
"partners": {
"101": {
"name": "xyz.com",
"prices": {
"1001": {
"description": "SingleRoom",
"amount": 125,
"from": "2012-10-12",
"to": "2012-10-13"
},
"1002": {
"description": "DoubleRoom",
"amount": 139,
"from": "2012-10-12",
"to": "2012-10-13"
}
}
}
}
}
要使用PHP访问JSON,您只需加载文件并将JSON转换为数组即可.
To access the JSON with PHP you can simply load the file and convert the JSON to an array.
<?php
$jsonFile = "data.json"
$json = file_get_contents($jsonFile);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
这篇关于如何写一个json文件作为PHP中的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!