PHP json_encode issue
PHP json_encode issue
我有一堆值和一个PHP数组,我需要将其转换为JSON值,以便通过CURL发布到parse.com
问题是PHP数组被转换为JSON对象(字符串作为键和值,而字符串只是值)
我最终使用
{"showtime":{"Parkne":"1348109940"}}
而不是
{"showtime":{Parkne:"1348109940"}}
parse抱怨这是一个对象而不是数组,因此不会接受它
作为
{"showtime":{"Parkne":"1348109940"}}
是JSON对象(key = a string
)
是否有使用json_encode
执行此操作的方法?还是一些解决方案?
这是JSON规范:必须引用对象键。虽然您的第一个未引用的版本是有效的Javascript,但引用的版本也是有效的,并且两者在任何Javascript引擎中的解析都是相同的。但在JSON中,键必须被引用。http://json.org
跟进:
显示如何定义数组,除非上面的示例是您的数组。这一切都归结为如何定义要编码的PHP结构。
// plain array with implicit numeric keying
php > $arr = array('hello', 'there');
php > echo json_encode($arr);
["hello","there"] <--- array
// array with string keys, aka 'object' in json/javascript
php > $arr2 = array('hello' => 'there');
php > echo json_encode($arr2);
{"hello":"there"} <-- object
// array with explicit numeric keying
php > $arr3 = array(0 => 'hello', 1 => 'there');
php > echo json_encode($arr3);
["hello","there"] <-- array
// array with mixed implicit/explicit numeric keying
php > $arr4 = array('hello', 1 => 'there');
php > echo json_encode($arr4);
["hello","there"] <-- array
// array with mixed numeric/string keying
php > $arr5 = array('hello' => 'there', 1 => 'foo');
php > echo json_encode($arr5);
{"hello":"there","1":"foo"} <--object
盲射。。。我的印象是,您的PHP数据结构不是您想要开始的数据结构。你可能有这个:
$data = array(
'showtime' => array(
'Parkne' => '1348109940'
)
);
实际上需要这个:
$data = array(
array(
'showtime' => array(
'Parkne' => '1348109940'
)
)
);
请随意编辑问题并提供预期输出的示例。
您可以使用JSON_encode将数组转换为JSON假设你的数组不是空的,你可以这样做
$array=();
$json = json_encode($array);
echo $json;
听起来您需要将单个对象封装在一个数组中。
试试这个:
// Generate this however you normally would
$vals = array('showtime' => array("Parkne" => "1348109940"));
$o = array(); // Wrap it up ...
$o[] = $vals; // ... in a regular array
$post_me = json_encode($o);
相关文章:
- issue with FB.Event.subscribe
- 使用Jquery Issue垂直重新排序Divs
- 创建了一个选项卡,但点击时不会起作用-jQuery issue/newbie
- jQuery find() Issue
- $route.reload(); Issue
- PHP json_encode issue
- Three.js issue creating meshes outside of loader's load(
- Jquery :gt(value) issue
- pickaday maxDate issue
- Stage.load(json) Issue..Kinetic.js
- Mootools Selector issue
- Java 8 Update 91 Issue
- JS parseInt() issue
- Jquery Colorbox issue with firefox
- Javascript firefox issue
- JavaScript Getters and Setters issue
- javascript and css firefox issue
- WordPress Blog - jQuery Issue
- Phantomjs page.evaluate and q deferred issue
- javascript中是否有类似utf8-encode(php)的函数