未捕获到的SyntaxError:JSON中位置36处的意外令牌p [英] Uncaught SyntaxError: Unexpected token p in JSON at position 36

查看:150
本文介绍了未捕获到的SyntaxError:JSON中位置36处的意外令牌p的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用sharepoint填充网页,所以我做了一个json以使用如下的ajax获取数据:

I´m populating webpage with sharepoint so I do a json to get data with ajax like these:

function completeFleet(data, target, eng) {
var items = data.d.results;
console.log(items);
var prefix = "<div class='row'>";
var sufix = "</div>";
var menu = "<div class='col-md-4'>";
var cat = "";
var spec = "";
var counter = 0;
var obj = null;
for (item in items) {
    spec = "";
    if (counter == 1) {

        menu += "</div><div class='col-md-4'>";
        counter = 0;
    }
    if (eng) {
        obj = JSON.parse(items[item].Specifications);
        for (var key in obj) {
            spec += "<div class='row'><div class='col-md-12 ftBottomSeparator'><span class=' t10'>" + key + "</span>&nbsp;<span class='t06' style='float:right;'>" + obj[key] + "</span></div></div>";
        }
        menu += "<div class='row ftContainerOut'><div class='col-md-12 ftContainer'><div class='row ftHeader'><div class='col-xs-9 t09'>" + items[item].Title + "</div><div class='col-xs-3 text-right'></div></div><div class='row'><div class='col-md-6' style='padding-top:10px'><img src='" + items[item].Imagen.Url + "' class='img-responsive img-center' style='border:0px solid blue; max-width:150px;max-height:120px;' /></div><div class='col-md-6'>" + spec + "</div></div></div></div>";
    } else {
        obj = JSON.parse(items[item].Especificaciones);
        for (var key in obj) {
            spec += "<div class='row'><div class='col-md-12 ftBottomSeparator'><span class=' t10'>" + key + "</span>&nbsp;<span class='t06'  style='float:right;'>" + obj[key] + "</span></div></div>";
        }

        menu += "<div class='row ftContainerOut'><div class='col-md-12 ftContainer'><div class='row ftHeader'><div class='col-xs-9 t09'>" + items[item].Title + "</div><div class='col-xs-3 text-right'></div></div><div class='row'><div class='col-md-6' style='padding-top:10px'><img src='" + items[item].Imagen.Url + "' class='img-responsive img-center' style='border:0px solid blue; max-width:150px;max-height:120px;' /></div><div class='col-md-6'>" + spec + "</div></div></div></div>";
    }
    counter++;
}
$(target).html("<div class='panel-body'><div class='container-fluid'>" + prefix + menu + sufix + "</div></div>");

}

我有5个不同的对象,但是其中一个不显示数据,我的网页是英语和西班牙语,用英语收取所有数据,但是用西班牙语中的一个 无效,位置36出现错误,位置36是不显示的项目.知道这里有什么问题吗?问候

I have 5 objects different, but one of these don´t show data, my webpage is in english and spanish, in english it charge all data, but in spanish one of these doesn´t works and I get error at position 36, and position 36 is the item don´t show. Any idea what is wrong here? Regards

这些作品

这没有用

---------更新------------

---------Update------------

如果我对此行发表评论:

If I comment this line:

 //obj = JSON.parse(items[item].Especificaciones);

放入

 if(items[item].Especificaciones){
 JSON.parse(items[item].Especificaciones);
   }

它现在与图像一起运行,但是现在我没有"Especificaciones"列表

it now runs with image, but now I don´t have my "Especificaciones" lists

现在使用时

var stringifyObj = JSON.stringify(items[item].Especificaciones); 
var obj = JSON.parse(stringifyObj);

我得到这样的东西:

推荐答案

确保JSON.parse中相应键的值不为null.例如-

make sure value is not null for the corresponding key inside JSON.parse. For example-

JSON.parse(items[item].Specifications)

确保项目在项目索引中具有价值,并且项目[item]具有规格"属性.

make sure items have value in item index and items[item] has the property Specifications.

您可以检查items [item].JSON.parse之前的规范不为null.

you can check if items[item].Specifications is not null before JSON.parse.

if(items[item].Specifications){
  JSON.parse(items[item].Specifications)
}

更新

JSON.parse()用于将包含JSON表示法的字符串转换为Javascript对象.要使用有效的JSON,字符串必须用双引号引起来.

JSON.parse() is used to convert a string containing JSON notation into a Javascript object. To be valid JSON, strings must be in double quotes.

尝试对对象进行字符串化,然后再次解析.

Try stringify the object and then parse again.

var stringifyObj = JSON.stringify(items[item].Especificaciones); 
var obj = JSON.parse(stringifyObj);

错误的原因是JSON.parse()需要一个String值和items [item].Especificaciones是一个数组

The reason for the error is that JSON.parse() expects a String value and items[item].Especificaciones is an Array

这篇关于未捕获到的SyntaxError:JSON中位置36处的意外令牌p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆