我正在创建一个插件,用户可以在其中添加自定义设置data-
HTML 中的属性。设置采用 JSON 格式。我在 Javascript 中使用这些设置。
它有preview
, base
and paths
特性。preview
and base
有字符串值,但是paths
是一个数组path
对象。
当我将 JSON 设置直接添加到 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}]"}'>
我正在使用这个值data('Params')
jQuery 的方法。它的类型是object
.
现在,当我试图json_encode
一个 php 数组并将其传递给data-
,添加成功
<div data-side="front" data-params=<?php echo "'".json_encode($dataParams)."'"; ?>>
但现在 typeofdata('Params')
在 JavaScript 中是string
。所以,我收到 JSON 解析错误。如果我删除paths
key,其类型变为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
)
)
那么,如果我包含,为什么它会将其类型更改为字符串paths
钥匙?有什么办法解决吗?
Edit:
这是输出: