我目前有一个通过输入数组值生成的JSON编码字符串,如下所示 -
"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"
我需要将其转换为以下格式:
"[{value: 97049},{value: 84866},{value: 39402},{value: 30250},{value: 33363}]"
感谢。
答案 0 :(得分:3)
$input = $json_var;
$input = str_replace( '"', '', $input ); // strip em
$input = '"' . $input . '"'; // wrap back around
答案 1 :(得分:2)
JS:
var json_array = JSON.parse(json_string);
for (var i = 0; i < json_array.length; i++) {
json_array[i] = JSON.parse(json_array[i];
}
PHP:
$json_array = json_decode($json_string);
$json_array = array_map('json_decode', $json_array);
最好在源头修复此问题。如果它应该是一个对象数组,在将它们添加到数组之前不要引用每个数组元素。
答案 2 :(得分:0)
var myQuotedJson = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
var myUnquotedJson = myQuotedJson.replace(/"/, '');
答案 3 :(得分:0)
你可以这样做:
var input = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
output = '"' + input.replace('"','') + '"';
//Alerts your output
alert(output );