Uncaught SyntaxError: Unexpected token p in JSON at position 36

感情迁移 提交于 2019-12-24 06:39:31

问题


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>");

}

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

These one works

and this no works

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

If I comment this line:

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

and put

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

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

Now when I use

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

I get something like these:


回答1:


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

JSON.parse(items[item].Specifications)

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

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

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

Update

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);

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




回答2:


"Eslora":100 pies"

You should probably opening the quotes when you start writing a string value



来源:https://stackoverflow.com/questions/37932677/uncaught-syntaxerror-unexpected-token-p-in-json-at-position-36

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!