从json_encode脚本创建数组,不带双引号

时间:2022-09-15 13:11:15

*This question was created as I had no control over the JSON output at the time. So had to use JavaScript. If you have control over the JSON, refer to Mike Brant's answer. But Oka's answer solved my issue below and is a great solution..

*这个问题是因为我当时无法控制JSON输出而创建的。所以不得不使用JavaScript。如果您可以控制JSON,请参阅Mike Brant的回答。但奥卡的答案解决了我的问题,是一个很好的解决方案..

I'm trying to create an array that doesn't contain double quotes from JSON.

我正在尝试创建一个不包含JSON双引号的数组。

I'm getting JSON and trying to build a system where I can push non quoted items to the array.

我正在获取JSON并尝试构建一个系统,我可以将非引用的项目推送到数组。

As I have no control over the JSON, I'm making it into a string and removing the double quotes and splitting it into an array again.

由于我无法控制JSON,我将其变为字符串并删除双引号并再次将其拆分为数组。

The problem is this still outputs the double quotes?

问题是这仍然输出双引号?

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';
var artistIds = artistJSON.replace(/"/g, '');
var artistAry = artistIds.split(',');
console.log(artistJSON);
console.log(artistIds);
console.log(artistAry);

Results from console;

控制台的结果;

["31","41","56","38","","27"]
[31,41,56,38,,27] //This is a string. I want an array.
["[31", "41", "56", "38", "", "27]"]

https://jsfiddle.net/1pu6nqu2/

Any help would be very grateful.

任何帮助都会非常感激。

*Just to confirm, my aim of the game is to remove the double quotes from within the array.

*只是为了确认,我的游戏目标是从数组中删除双引号。

5 个解决方案

#1


Assuming you're trying to turn stringified JSON into an array, and turn the strings inside the array into numbers. You can use some combination of .map() and .filter() to achieve this.

假设您正在尝试将字符串化的JSON转换为数组,并将数组中的字符串转换为数字。您可以使用.map()和.filter()的某种组合来实现此目的。

http://jsbin.com/yojibeguna/1/edit?js,console

var artistJSON = JSON.parse('["31","41","56","38","","27"]')
                     .map(function (e) { return parseInt(e); })
                     .filter(function (e) { return isFinite(e); });

console.log(artistJSON, typeof artistJSON[0]);

#2


If you are using json_encode() from PHP to dynamically populate the data structure, you should not populate into a string and then parse that string, just write directly to object/array literal. So change this:

如果您使用PHP中的json_encode()动态填充数据结构,则不应填充到字符串中然后解析该字符串,只需直接写入对象/数组文字。所以改变这个:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';

to this

var artist = <?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>;

All I did was remove the single quotes (and change the variable name to something more appropriate). Now you have a data structure you can work with directly in javascript without need for additional parsing.

我所做的就是删除单引号(并将变量名更改为更合适的名称)。现在你有了一个数据结构,你可以直接在javascript中使用,而无需额外的解析。

#3


If the json data is stored as JSON data already, you do not need to re-encode it with php. Just echo it out and it will be assigned to your variable artistJSON.

如果json数据已经存储为JSON数据,则无需使用php对其进行重新编码。只需将其回显,它就会被分配给您的变量artistJSON。

Example:

var artistJSON = <?php echo $favourites ? ($favourites->artists) : '[]' ?>;

Edit: As Mike Brant said, you do need to re-encode it if it's not already stored as JSON literal data (in a db, or whatnot). I'm assuming it is.

编辑:正如Mike Brant所说,如果它尚未存储为JSON文字数据(在数据库中,或者诸如此类),则需要对其进行重新编码。我假设是。

#4


You can just run JSON.parse to convert the string to an array.

您可以运行JSON.parse将字符串转换为数组。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

#5


Try this:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';
var artists = JSON.parse( artisanJSON );

console.log( artists );

REF: How to json_encode php array but the keys without quotes

REF:如何json_encode php数组,但没有引号的键

#1


Assuming you're trying to turn stringified JSON into an array, and turn the strings inside the array into numbers. You can use some combination of .map() and .filter() to achieve this.

假设您正在尝试将字符串化的JSON转换为数组,并将数组中的字符串转换为数字。您可以使用.map()和.filter()的某种组合来实现此目的。

http://jsbin.com/yojibeguna/1/edit?js,console

var artistJSON = JSON.parse('["31","41","56","38","","27"]')
                     .map(function (e) { return parseInt(e); })
                     .filter(function (e) { return isFinite(e); });

console.log(artistJSON, typeof artistJSON[0]);

#2


If you are using json_encode() from PHP to dynamically populate the data structure, you should not populate into a string and then parse that string, just write directly to object/array literal. So change this:

如果您使用PHP中的json_encode()动态填充数据结构,则不应填充到字符串中然后解析该字符串,只需直接写入对象/数组文字。所以改变这个:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';

to this

var artist = <?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>;

All I did was remove the single quotes (and change the variable name to something more appropriate). Now you have a data structure you can work with directly in javascript without need for additional parsing.

我所做的就是删除单引号(并将变量名更改为更合适的名称)。现在你有了一个数据结构,你可以直接在javascript中使用,而无需额外的解析。

#3


If the json data is stored as JSON data already, you do not need to re-encode it with php. Just echo it out and it will be assigned to your variable artistJSON.

如果json数据已经存储为JSON数据,则无需使用php对其进行重新编码。只需将其回显,它就会被分配给您的变量artistJSON。

Example:

var artistJSON = <?php echo $favourites ? ($favourites->artists) : '[]' ?>;

Edit: As Mike Brant said, you do need to re-encode it if it's not already stored as JSON literal data (in a db, or whatnot). I'm assuming it is.

编辑:正如Mike Brant所说,如果它尚未存储为JSON文字数据(在数据库中,或者诸如此类),则需要对其进行重新编码。我假设是。

#4


You can just run JSON.parse to convert the string to an array.

您可以运行JSON.parse将字符串转换为数组。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

#5


Try this:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';
var artists = JSON.parse( artisanJSON );

console.log( artists );

REF: How to json_encode php array but the keys without quotes

REF:如何json_encode php数组,但没有引号的键