2017-05-18 28 views
-4

JS:JSON解析エラーの解決方法?

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リンク:https://jsbin.com/rarizavocu/edit?js,output

フッター注:このJSONは、PHPの関数json_encodeを使用して生成されます。

クロームコンソールのエラー:それは働いていない理由の位置でJSONに予期しない数41

$ .parseJSONは、いずれかが私を助けてくださいすることができます動作しませんか?

+1

JSON.parse(応答)を使用するを使用する必要があります。 –

+3

これは複数行の文字列のようですが、複数行の文字列は文字列のテンプレートリテラルhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literalsを通してのみ使用できます。 JSOnは**二重引用符**を許可し、あなたは一重引用符を使用しています。 – Hitmands

+0

JSON.parseなどを使用する代わりに、JSで開始引用符と終了引用符を削除しようとしましたか? 'var data = response.replace(/(^ ')|(' $)/ g、 '');'のようなものです。あなたはそれを一度試すことができますがわかりません。ダウンボートの代わりに –

答えて

-1

あなただけ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

、あなたはそれに同意しない理由を説明しようとする。 – Hitmands

+0

私はdownvoteをしませんでしたが、ロジックを逆転したのでOPの問題には答えません。 *文字列をデコードするのではなく*配列をJSONにエンコードしています。 –

+0

lol ...それはちょうど例であり、 'json_encode'が正確に何をしているのです。私は私のコメントと私の答えにすべての問題を強調したと思う。 – Hitmands