我正在创建一个插件,用户可以在HTML的data-
属性中添加自定义设置.设置是JSON格式的.我在Javascript中使用这些设置.
它有preview
,base
和paths
处房产.preview
和base
具有字符串值,但是paths
是由path
个对象组成的array.
It works fine when i add JSON setting directly into the HTML:
<div data-side="front" data-params='{"preview": "assets/img/products/tshirt/front-preview.png", "base": "assets/img/products/tshirt/front-base.png", "paths": "[{\"name\": \"base\", \"path\": \"M 324.00,33.00 C 324.00,33.00\", \"x\": 92, \"y\": 16, \"height\": 370}, {\"name\": \"collar\", \"path\": \"M 358.00,46.10 C 358.00,46.10\", \"x\": 170, \"y\": 17, \"height\": 21}, {\"name\": \"leftSleeve\", \"path\": \"M 633.17,170.00 C 633.17,170.00\", \"x\": 288, \"y\": 66, \"height\": 131}, {\"name\": \"leftCuff\", \"path\": \"M 595.00,438.00 C 615.47,438.23\", \"x\": 293, \"y\": 172, \"height\": 33}, {\"name\": \"rightSleeve\", \"path\": \"M 142.00,140.00 C 143.46,150.28\", \"x\": 47, \"y\": 64, \"height\": 131}, {\"name\": \"rightCuff\", \"path\": \"M 48.00,375.38 C 48.00,375.38 95.00\", \"x\": 41, \"y\": 166, \"height\": 36}]"}'>
I'm getting this value using data('Params')
method of jQuery. Its type is object
.
Now, when i'm trying to json_encode
a php array and pass it to the data-
, it's added successfully
<div data-side="front" data-params=<?php echo "'".json_encode($dataParams)."'"; ?>>
But now typeof data('Params')
in Javascript is string
. So, i'm getting a JSON parse error. If i remove paths
key, its type changes to object.
下面是我正在编码的数组的print_r
:
Array
(
[preview] => assets/img/products/tshirt/front-preview.png
[base] => assets/img/products/tshirt/front-base.png
[paths] => Array
(
[0] => Array
(
[name] => base
[path] => M 324.00,33.00 C 324.00,33.00
[x] => 92
[y] => 16
[height] => 370
)
... and more path arrays
)
)
So, why does it changes its type to string if i include paths
key? Any way to solve it?
编辑:
以下是输出: