2017-08-07 91 views
1

我嘗試解析以下JSON字符串(生成並從服務器返回)使用JSON.parse:JSON.parse返回意外的標記

[{"id":1,"username":"klebermo","password":"$2a$04$YYqryKrcmUQfwmbsnhA1Te0FghiYsirxVq.Wf.kChjpEm/uuky.fu","firstName":"Kleber","lastName":"Mota","email":"[email protected]","credenciais":[{"id":1,"nome":"user","autorizacoes":[{"id":1,"nome":"user","authority":"user"}]},{"id":2,"nome":"admin","autorizacoes":[{"id":2,"nome":"admin","authority":"admin"}]}],"enabled":true,"credentialsNonExpired":true,"accountNonLocked":true,"accountNonExpired":true,"authorities":[{"id":1,"nome":"user","authority":"user"},{"id":2,"nome":"admin","authority":"admin"}]}] 

,但我得到這個錯誤:

Uncaught SyntaxError: Unexpected token o in JSON at position 1 
at JSON.parse (<anonymous>) 
at Object.success ((index):186) 
at i (jquery-3.2.1.min.js:2) 
at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2) 
at A (jquery-3.2.1.min.js:4) 
at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4) 

我的JavaScript代碼中這個解析shappens:

<script th:inline="javascript"> 
/*<![CDATA[*/ 
var atributos = []; 

$('.col').each(function(){ 
    var prop = $(this).data('prop'); 
    atributos.push(prop); 
}); 

var url = '/' + $('.list').data('list') + '/list.json'; 

$.get(url, function(data){ 
    var json = JSON.parse(data); 

    $.each(json.item, function(index, item){ 
     var row = $('<tr id='+item.id+'>'); 

     for(var i=0; i<atributos.length; i++) { 
      if(atributos[i] == '#') { 
       row.append('<td></td>'); 
      } else if(atributos[i] == '.') { 
       var col = $('<td>'); 
       col.append('<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modal_update">editar</button>'); 
       col.append('<button type="button" class="btn btn-primary btn-lg" class="remover" data-target="'+item.id+'">remover</button>'); 
       row.append(col); 
      } else { 
       row.append('<td>'+item[atributos[i]]+'</td>'); 
      } 
     } 

     $('table.table').find('tbody.list').append(row); 
    }); 
}); 
/*]]>*/ 
</script> 

如果我嘗試驗證JSON字符串在幾個JSON驗證的網站,並沒有錯誤顯示。 JSON列在瀏覽器開發工具的「網絡」選項卡中,並且它也正確顯示。

任何人都可以給出什麼可能是錯在這裏提示嗎?

+2

jQuery是做解析爲您服務。 – Pointy

+0

那個json對象最後有一個太多的方括號,是一個錯字? – Tim

+0

@Tim是的,它是。已經糾正它。 –

回答

3

這已經是一個對象。無需解析它。 o在位置1是從[object Object]第二符號。