2017-05-18 7 views
-4

JS:JSON Parsing Fehler, wie zu lösen?

var response = '[{ 
    "userid":"14", 
    "mobile":"a:1:{i:0;s:10:\"9925847424\";}", 
    "pet_type":"Found", 
    "pet_cat":"2", 
    "petbreed":"1", 
    "pet_long":"72.58522", 
    "pet_lat":"23.087618", 
    "pet_reward":"", 
    "currency":"\u20ac (EUR)", 
    "name":"", 
    "date":"2017-03-27", 
    "pet_city":"Los Angelese", 
    "pid":"200", 
    "lid":"194", 
    "type":"Found", 
    "pet_lost_location":"Santa Monica", 
    "pet_lost_address":" dshtjh", 
    "pet_postal":"380009", 
    "pet_country":"United states", 
    "distance":"0.15473920640658162", 
    "dogbreed":null, 
    "catbreed":"Abyssinian cat", 
    "img":"a:1:{i:0;s:9:\"img1.jpeg\";}", 
    "pet_hidenum":"249310649" 
}, 
{ 
    "userid":"13", 
    "mobile":null, 
    "pet_type":"Lost", 
    "pet_cat":"1", 
    "petbreed":"5", 
    "pet_long":"72.58522", 
    "pet_lat":"23.087618", 
    "pet_reward":"23", 
    "currency":"\u20ac (EUR)", 
    "name":"pappy", 
    "date":"2017-04-27", 
    "pet_city":"ahmedavad", 
    "pid":"233", 
    "lid":"227", 
    "type":"Lost", 
    "pet_lost_location":"sivranjani", 
    "pet_lost_address":"ahmedavad", 
    "pet_postal":" dmart", 
    "pet_country":"India", 
    "distance":"0.15473920640658162", 
    "dogbreed":"Aidi", 
    "catbreed":null, 
    "img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:9:\"img2.jpeg\";i:2;s:8:\"img3.png\";i:3;s:9:\"img4.jpeg\";}", 
    "pet_hidenum":"730117427" 
}, 
{ 
    "userid":"13", 
    "mobile":null, 
    "pet_type":"Found", 
    "pet_cat":"2", 
    "petbreed":"7", 
    "pet_long":"72.58522", 
    "pet_lat":"23.087618", 
    "pet_reward":"", 
    "currency":"\u20ac (EUR)", 
    "name":"", 
    "date":"2017-04-27", 
    "pet_city":"a", 
    "pid":"234", 
    "lid":"228", 
    "type":"Found", 
    "pet_lost_location":"asds", 
    "pet_lost_address":"asdsffas", 
    "pet_postal":"qwer", 
    "pet_country":"India", 
    "distance":"0.15473920640658162", 
    "dogbreed":null, 
    "catbreed":"Asian cat", 
    "img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:8:\"img2.png\";i:2;s:9:\"img3.jpeg\";i:3;s:8:\"img4.png\";}", 
    "pet_hidenum":"358512553" 
}, 
{ 
    "userid":"16", 
    "mobile":"", 
    "pet_type":"Lost", 
    "pet_cat":"2", 
    "petbreed":"511", 
    "pet_long":"72.58522", 
    "pet_lat":"23.087618", 
    "pet_reward":"1", 
    "currency":"$ (USD)", 
    "name":"Cade1", 
    "date":"1970-01-01", 
    "pet_city":"Ahmedabad", 
    "pid":"236", 
    "lid":"230", 
    "type":"Lost", 
    "pet_lost_location":"California", 
    "pet_lost_address":"12 San Diego", 
    "pet_postal":"380008", 
    "pet_country":"United states", 
    "distance":"0.15473920640658162", 
    "dogbreed":null, 
    "catbreed":"Unknown", 
    "img":"a:1:{i:0;s:9:\"img1.jpeg\";}", 
    "pet_hidenum":"501387450" 
}]'; 
res = $.parseJSON(response); 

JSBIN-Link: https://jsbin.com/rarizavocu/edit?js,output

Footer Hinweis: Diese JSON durch Verwendung von PHP Funktion json_encode erzeugt wird.

Chrome Console Fehler: Unerwartete Anzahl in JSON an Position 41

$ .parseJSON funktioniert nicht jeder kann mir bitte helfen, warum es nicht funktioniert?

+1

Verwendung JSON.parse (Antwort); –

+3

Es scheint ein mehrzeiliger String zu sein, mehrzeilige Strings sind nur über String-Template-Literale erlaubt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals. JSOn erlaubt ** doppelte Anführungszeichen ** und Sie verwenden einfache Anführungszeichen. – Hitmands

+0

Haben Sie versucht, Anfangs- und Endzitat in JS zu entfernen, statt JSON.parse usw. zu verwenden? etwas wie 'var data = response.replace (/ (^ ') | (' $)/g, '');'. Nicht sicher, aber Sie können es einmal versuchen. –

Antwort

-1

sollten Sie verwenden nur double quoted strings

var data = [{"userid":"14","mobile":"a:1:{i:0;s:10:\"9925847424\";}","pet_type":"Found","pet_cat":"2","petbreed":"1","pet_long":"72.58522","pet_lat":"23.087618","pet_reward":"","currency":"€ (EUR)","name":"","date":"2017-03-27","pet_city":"Los Angelese","pid":"200","lid":"194","type":"Found","pet_lost_location":"Santa Monica","pet_lost_address":" dshtjh","pet_postal":"380009","pet_country":"United states","distance":"0.15473920640658162","dogbreed":null,"catbreed":"Abyssinian cat","img":"a:1:{i:0;s:9:\"img1.jpeg\";}","pet_hidenum":"249310649"},{"userid":"13","mobile":null,"pet_type":"Lost","pet_cat":"1","petbreed":"5","pet_long":"72.58522","pet_lat":"23.087618","pet_reward":"23","currency":"€ (EUR)","name":"pappy","date":"2017-04-27","pet_city":"ahmedavad","pid":"233","lid":"227","type":"Lost","pet_lost_location":"sivranjani","pet_lost_address":"ahmedavad","pet_postal":" dmart","pet_country":"India","distance":"0.15473920640658162","dogbreed":"Aidi","catbreed":null,"img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:9:\"img2.jpeg\";i:2;s:8:\"img3.png\";i:3;s:9:\"img4.jpeg\";}","pet_hidenum":"730117427"},{"userid":"13","mobile":null,"pet_type":"Found","pet_cat":"2","petbreed":"7","pet_long":"72.58522","pet_lat":"23.087618","pet_reward":"","currency":"€ (EUR)","name":"","date":"2017-04-27","pet_city":"a","pid":"234","lid":"228","type":"Found","pet_lost_location":"asds","pet_lost_address":"asdsffas","pet_postal":"qwer","pet_country":"India","distance":"0.15473920640658162","dogbreed":null,"catbreed":"Asian cat","img":"a:4:{i:0;s:9:\"img1.jpeg\";i:1;s:8:\"img2.png\";i:2;s:9:\"img3.jpeg\";i:3;s:8:\"img4.png\";}","pet_hidenum":"358512553"},{"userid":"16","mobile":"","pet_type":"Lost","pet_cat":"2","petbreed":"511","pet_long":"72.58522","pet_lat":"23.087618","pet_reward":"1","currency":"$ (USD)","name":"Cade1","date":"1970-01-01","pet_city":"Ahmedabad","pid":"236","lid":"230","type":"Lost","pet_lost_location":"California","pet_lost_address":"12 San Diego","pet_postal":"380008","pet_country":"United states","distance":"0.15473920640658162","dogbreed":null,"catbreed":"Unknown","img":"a:1:{i:0;s:9:\"img1.jpeg\";}","pet_hidenum":"501387450"}]; 
 

 

 
var stringified = JSON.stringify(data); 
 
console.log(stringified); 
 
// this is a valid JSON String, now you can JSON.parse(stringified)

+0

anstatt zu verwerfen, versuchen Sie zu erklären, warum Sie damit nicht einverstanden sind. – Hitmands

+0

Ich habe nicht heruntergestimmt, aber das beantwortet das OP-Problem nicht, da Sie die Logik umgekehrt haben. Sie * codieren * das Array zu JSON, nicht * dekodieren * die Zeichenfolge. –

+0

lol ... Es war nur ein Beispiel, genau das macht 'json_encode'. Ich denke, ich habe alle Punkte in meinem Kommentar und in meiner Antwort hervorgehoben. – Hitmands