简体   繁体   English

将 PHP 关联数组转换为 JSON 对象 - 值中的空格会导致 JSON 格式错误

[英]Converting PHP Associative array to JSON object - spaces in value results in malformed JSON

I build up an array in PHP and convert it to JSON which I then handle with an onclick event and send to another file via AJAX.我在 PHP 中构建了一个数组并将其转换为 JSON,然后我使用 onclick 事件处理并通过 AJAX 发送到另一个文件。

The problem is that some of the values in the php array have spaces in them.问题是 php 数组中的某些值中有空格。 This seems to be breaking the JSON object meaning my AJAX call is failing.这似乎破坏了 JSON 对象,这意味着我的 AJAX 调用失败了。

Here's a snippet of code to try and elaborate:下面是一段代码,可以尝试详细说明:

//PHP Array looks like this:

            Array
            (
                [card_id] => 1
                [img_id] => 11
                [card_name] => Layout1retro_original
                [card_qty] => 1
                [img_thumb] => albums/160915_E165/thumbs/011_cover-lp-cd_originalDPP_Polaroid.jpg
                [img_hires] => 
                [img_full] => albums/160915_E165/images/011_cover-lp-cd_originalDPP_Polaroid.jpg
                [media] => retro
                [finish] => Perl290
                [size] => original
                [backing] => 
                [can_crop] => 
                [needs_to_be_cropped] => 1
                [been_cropped] => 
                [aspect_ratio] => 0
                [offer_CMF] => 
                [retro-name] => A value
            )

The problem key => val pair is the retro-name .问题key => val对是retro-name The JSON is breaking where the space is between A and value . JSON 打破了Avalue之间的空间。

I use json_encode to convert the array to a JSON object to pass with Javascript: stripslashes(json_encode($array)) .我使用json_encode将数组转换为 JSON 对象以通过 Javascript 传递: stripslashes(json_encode($array))

Then I add it to an html element as a data attribute so I can grab it with the onclick event.然后我将它作为data attribute添加到 html 元素中,以便我可以使用onclick事件获取它。

Here's where it is breaking in the data attribute: [removed excess code for clarity...] "retro-name":"A" value"}这里是数据属性中断的地方: [removed excess code for clarity...] "retro-name":"A" value"}

Notice it has added a closing " after A . This breaks the rest of the JSON object so my AJAX call fails.请注意,它在A之后添加了一个结束" 。这会破坏 JSON 对象的其余部分,因此我的 AJAX 调用失败。

How can I fix this?我怎样才能解决这个问题?

My best guess is because the JSON is rendered in a data attribute enclosed in double-quotes, as is common in HTML.我最好的猜测是因为 JSON 是在用双引号括起来的数据属性中呈现的,这在 HTML 中很常见。 Instead of double-quotes, please try single-quotes.而不是双引号,请尝试单引号。 PHP outputs JSON with double-quotes. PHP 输出带双引号的 JSON。

If this does not work, please can you update your question with an example of how the data-attribute is rendered with the JSON.如果这不起作用,请您举例说明如何使用 JSON 呈现数据属性来更新您的问题。

If I were to take a guess, it's stripslashes() that is removing a slash in front of the (presumed) escaped " after A in A" value .如果我要猜测,它是stripslashes()删除(假定)转义的" A in A" value之后的斜线。 I'm almost certain there's a \\ in front of the " in the original data, that you are not showing us verbatim.我几乎可以肯定,原始数据中的"前面有一个\\ ,您没有逐字向我们展示。

A different approach:一种不同的方法:

Just put the card_id in the card div只需将card_id放在卡片 div 中

<div class="print-wrap has-size" data-card_id="<?=$array['card_id'];?>">

and then pass the card data into a variable in JS然后将卡片数据传递给JS中的一个变量

<script>var card_data['<?=$array["card_id"]?>'] = <?=json_encode($array);?>;</script>

Then you have a relation to your data and the div.然后你有你的数据和 div 的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM