gpt4 book ai didi

javascript - parseJSON 在一种情况下有效,在另一种情况下无效

转载 作者:行者123 更新时间:2023-12-03 10:28:31 25 4
gpt4 key购买 nike

我使用ajay从服务器获取一些JSON数据,通过将数组传递给PHP的json_enocode(),它返回一个有效的JSON(在许多在线JSON调试器上检查过)。但是 JS 的 $.parseJSON() 不接受它。解析器对一组数据抛出错误(如下),而对其他数据集则正常工作。

CONSOLE> SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

我尝试一一删除数组的所有元素,但即使只处理单个元素也不起作用。以下是数据集:

这正在工作:

[{"id":"54a3b103877c0","act_name":"ABC","profile_image":"[\"551d\"]"}]

虽然这不是:

[{"id":"2","user_id":"53b595a","review_for_id":"54f25","review_for_type":"city",
"r_title":"asfasas asd as","r_body":"a sdasda sdas dasdas das d","r_rating":"3",
"other_info":"","added":"2015-03-28 15:47:20","modified":"2015-03-28 15:47:20",
"f_name":"ABC","l_name":"XYZ","lives_in":"ABC"}]

PHP 代码:

数组:

Array
(
[0] => Array
(
[id] => 2
[user_id] => 53b595a
[review_for_id] => 54f25
[review_for_type] => city
[r_title] => asfasas asd as
[r_body] => a sdasda sdas dasdas das d
[r_rating] => 3
[other_info] =>
[added] => 2015-03-28 15:47:20
[modified] => 2015-03-28 15:47:20
[f_name] => ABC
[l_name] => XYZ
[lives_in] => ABC
)

)

PHP-以上数组包含在 $d 中

json_encode($d, JSON_HEX_TAG | JSON_HEX_QUOT)

这是用于 Ajax 的 Jquery:

 $.get(url, '', function(d){
var data = {};
if(d != ''){
data = $.parseJSON(d);
}
var acc = {};
acc[sc_id] = data;
console.log(acc);
});

请帮忙!

最佳答案

<script>
var str = '[{"id":"2","user_id":"53b595a","review_for_id":"54f25","review_for_type":"city","r_title":"asfasas asd as","r_body":"a sdasda sdas dasdas das d","r_rating":"3","other_info":"","added":"2015-03-28 15:47:20","modified":"2015-03-28 15:47:20","f_name":"ABC","l_name":"XYZ","lives_in":"ABC"}]';
var obj = $.parseJSON(str);
alert(obj.id);
</script>

关于javascript - parseJSON 在一种情况下有效,在另一种情况下无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com