【问题标题】:how to fix Uncaught SyntaxError: Unexpected token o in JSON at position 1 [duplicate]如何修复未捕获的 SyntaxError:位置 1 处 JSON 中的意外标记 o [重复]
【发布时间】:2019-11-08 05:45:18
【问题描述】:
$.ajax({
                url: "/url/url.ajax?length=100000&startDate=2018-07-01",
                method: "get",
                dataType: "json",
                success: function (jdata) {
                    var jsonData=JSON.parse(jdata.data);
                }
            });

响应 Json:

{startIndex: 0, draw: 0, recordsTotal: 22, recordsFiltered: 22, pageNumber: 0,…}
data: [{,…}, {,…}, {,…}, {,…},…]
0: {,…}
1: {,…}
2: {,…}
3: {,…}
4: {shequData: {specialFlag: null, userCode: "17110000003", passportNo: "T02221", id: null,…},…}
5: {,…}
6: {,…}
7: {,…}
8: {,…}
9: {,…}
10: {,…}
11: {,…}
12: {,…}
13: {,…}
14: {shequData: {specialFlag: null, userCode: "ZY88888", passportNo: "Z01917", id: null,…}, remark: null,…}
15: {,…}
16: {,…}
17: {,…}
18: {,…}
19: {,…}
20: {,…}
21: {,…}
draw: 0
pageNumber: 0
recordsFiltered: 22
recordsTotal: 22
startIndex: 0

json 有超过 100000 个字符,所以我发布了 json 的预览

对于var jsonData=JSON.parse(jdata.data);,我收到错误 Uncaught SyntaxError: Unexpected token o in JSON at position 1 如何解决此错误?

【问题讨论】:

  • 你解析了两次,分别是dataType: jsonJSON.parse()
  • @Kobe 是对的,请参阅重复的帖子。您不必要地解析 JSON 两次。

标签: javascript jquery json


【解决方案1】:

不用再解析了,已经是json了

$.ajax({
                url: "/url/url.ajax?length=100000&startDate=2018-07-01",
                method: "get",
                dataType: "json",
                success: function (jdata) {
                    var jsonData=jdata.data;
                }
            });

$.ajax({
                url: "https://jsonplaceholder.typicode.com/posts",
                method: "get",
                dataType: "json",
                success: function (jdata) {
                    var jsonData=jdata
                    console.log(jsonData)
                }
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

【讨论】:

    【解决方案2】:

    您是否尝试过删除 JSON.parse();

    所以只需执行以下操作并直接使用它。它已经是 JSON。

       var jsonData=jdata;
    

    你可以试试这样的东西来测试它。

      console.log(jdata.recordsFiltered);
    

    很抱歉没有将此作为评论,我没有足够的分数所以不允许评论。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2019-01-03
      • 2021-08-28
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多