JSON Parse使用base64编码的图像失败Javascript

时间:2016-01-06 20:04:52

标签: javascript json base64

我正在尝试解析包含base64编码图像的JSON字符串。 我检查了JSON格式,这是正确的。为什么解析失败了?是否与base64中的特殊字符有关?

控制台出错:"语法错误:JSON解析错误:意外的EOF"

我现在拥有的:

var pic = new XMLHttpRequest();
pic.open("GET",  api+"getimage?"+"userID="+myUserID, true);
pic.send();
var picArray= JSON.parse(pic.response);

JSON响应是:     {"图像":" / 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wCEAAkGBxISEhUSEhIVFRUVFRUVFRUVFRUVFRUVFhUWFhUVFhUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OFxAPFy0lHR0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf / AABEIALsBDgMBIgACEQEDEQH / xAAcAAACAgMBAQAAAAAAAAAAAAAAAQIFAwQGBwj / XAA / EAACAgECAwYEAwUGBQUAAAABAgADEQQhBRIxBhMiQVFhMnGBkQdCoRQjYpKxUnKCosHwM2OD0eEWQ5PC8f / EABgBAQEBAQEAAAAAAAAAAAAAAAABAgME / 8QAHREBAQEAAgIDAAAAAAAAAAAAABEBEiECQTFRYf / aAAwDAQACEQMRAD8A9YAhiSxDEyhYjjhiQRxJYgBHAjiElAQpQxJYixAWI4EwgKEeIQFAxmEBQgY4ChCOAsQhHiAoRwgAihiOAoGPEJQRwhCCEBHiAhHDEMQIAR4ksRSBR4hiEKMQxHCAYijgYCikgIQFiEcMQDEUcJAoR4hiFRhiShKFDEIQCKPEIBAwxCARRwxAUeIRiELEI4SghGBCAo4QxCFCMiAEiiKBjgKOKEAzK / jfGKdJUbrmwo2AHxMfJVHmZvzwb8ReMPqdZYOcNVUxrrAOw5cczfMtnf29pPweocM / EPh92B33dH0tUp / m3X9Z02n1CWDmRldT5qQw + 4nzFyzNpdRZUeap3rb1Rip + 4M1xH05CeFcN / EXiFOxsW4elq5P8y4P9Z1nDPxbqO2o07ofNq2Fi / Y4I / WSar0mGJR8L7YaHUYFepr5j0Vz3bfRXxn6S8BzvIDEI4YgRxCOOBHEI4QFCOEoUWJLEMQAQxCOBGMRmAEBQxHCAYijhiAhHiGI4QopLEiYChAwgBmlxbidemp E + 04RBk + ZJJwAB5kkgD5zbJnn / AOMuq5dCqb / vL0G38Ku // wBRJo4HtF2y1WrtLix6qxkJWjsoC / xcpHMx8zOfAmAuVODkH0Iwf1khfN5mYjPAzELhMgIMoic + X6xFW9j + kyGAgQ5c + UseGca1Wn / 4Gosr9FDEr / KFD + k01QnfyHrtJ2rjqOvTfaZ2K7Thv4payvAuSu4bb47tz75Xb / LOs4Z + Keis2tW2g / xLzrn + 8mT9wJ42T / V / ALQ5​​fUZ3 + / tJCvpHhvGNPqBmi + uz2VwT9V6ibs + YOXBBGxHQ9Dn2PlL3hnbDiGnwE1Lso / LB + 9X5ZfJH0IiLX0HCeT8N / FuwYGp0wb1apip / kfIP8wnXcM / ETh92P33dH0uUp / n3X9ZB1UMSFF6OoZGV1PRlIYH5EbTJAUIRwFARwgGIQEcoUIQEAEcUcAixHCApExyJEAMixjmN4EbLJz / afSrqKTWeoKup8wyMGBH2x9ZbX5lPriYR5V2709jCojLgF84XJHw7kjfE2X7LaRkHitrYKMkYdWIG59QM5OP1l3xFihO3lkH2JIP9Jq06xWUsTgDZvLBxLmddDgOE8Mu1LMtCd4VUsQCM4yB59TuJDWaS2k4tres + XMCM / I9DOm7JUojWhfRSPoSNvvH2 + e1xTklgOc4Jzj4Zexyi6g / OZV1A8xO8o4DoGqrSyh1YIoNtT7ucbuynbJ6zieHcIfU6g0UEHd8GwhMKufiPTJwB8zFE6NQMEYBB + / + 3jZ8nOPPOI MdnNVpBzX0si5xz7FSfLBErEvPr / rHQseYHy / 37xhPQ9Os001PqJlW5T54 + cIzZPn0PX / WNiuJAHzjJ2hSLSJEcMyoyaPVWUtzVWPW3qjFCfmVO86nhn4k8QqwGdLl9LUGcezJynPuczkoYkmK9a4X + LdDYGoosrP8AarIsX7HlP6GdfwrtZodTgVamsseiMe7f + R8H9J864kHx54k4lfUwgZ828M7RavT / APA1NiAfl5 UZP / JBK / pOu4Z + LGqTa + mu4eq5qf8ATKn7CSateywnEcL / ABQ0Fu1hsoP / ADFyv0ZM7fPE63QcRpvHNTbXYPVHVvvg7SDZjhASghHFABCOECJkTNW3VkkrUAxHxMfgX6 + Z9hMLVD8xLn3Jx9FG0ueKVvRGaQRR0VR / hEjyYO2R8jj9OkvEraevM0dToMiS1fEBUvO5yBtsPExOwVQOpPpMtXEUyqt4HYZCNuQB1yRkDGcZzJNLjkOKcFdmA5SNjk42x5bypfgYVLE38S5HuV3npbgNvkH9ZqX8NrbcjB9RtJ6I8To4dalgZcgZGcea53EfaytxZy9VXddtyGAOTPWL + ArjCj7yi4n2ZZ2HOAAq4yMb4Jx / WWkcvwniKmoO2wQBX5umQAOo8pTdlkT9osxsGRzv5 + IEbTrrOCciWVgfEufbw5M5UcEtVudMgjcETWb2jb7Zc50qqrHlNqnl5jjZX3x5dZsdmtDpf2VVv0qW8xLl + YpaCdsBhtgco2 + c1O1tbhgo + DAZRjfz8 / vMvZzWk1GsgjugWORsVJJJ9dsyTFc12j0FS6s06UOq + AKthyeZgDgt9RLLiX4f66pS61rfWN + el1cAepGc / wBYnurfXC1ehsTffpgL5zptXYVqtKMVPdv8JK5ypG + PnE30PMEt9DM6ao + E / wCk6fsHQiPY91Nd2ECclm64f4j8 / CBn3My9u9FoUrR9PQ9FjWYYd4XrK8pPhBO24HkIo5hdQD7TKGB88 / LeWvBew2r1VHf0d027Duy4W0gYwwU + ROepHSUnFOHXaWzu762qcDPK2xwc4ORsRsYpGaE011Deufn / AN5lTUjzBHy3lRnkQogtgPQ / 7 + UlAia8 + 3uOskVhmGYC5Y6iVYMpKsOjKSrD5EbiGYGB0vDO3vEaNhebFH5bh3g / MPJ / AM07Tsz + KXf3V0X6blax1rD1NleZjgEo24Gf4jPJSZ0n4d8Pa3W0kflsRs + gQixj9lx / imfLMivoCM QhIojxEI4FfgKAqjCjoBMcGiZp2YBmDW6tKkLucAY9ySdgoHUknYASm / 8AU3euyaSltRyHD2cy10g + gsb4j / dBmTgvF01T2I9RS7TOA6tytyllOGRxscjPoZINjS6axz39oHPg91WTlagR5kdXPmR06DzJxNorVHKhzZbvdqNvCB + WtT0O5CjoNycnrc4igc / TaqAshNGlpJywyGucHc + pXm89y7e3xblfGbVQPYgLWtimj4bMY252GQDjxNthem / nm4vqK0QNYvPhh3agZZrPyBAfzf03OwGZg0WitAe1yv7S6kAnLV0j8tajYlQcEnYsfQYAkvyLOviVZc1nIcKGYYJCg7DLDYZwcZwTgzYr5XGVKsPUEEbbHce859uGOFFFZYLYS + ov5gLHOwYAjcO3TIwFUYGNsYktTe7ejS6fZAuUNpXw83Ku5QfCq / MO + 45c53xxavtRw2t + Q7 + o2M0NRwRSMDAmKrimoRFaxQbLmxVpzgOoxsHddth4mOCB0Gds2S8UTn7tgwcKGbA5lUE4BYjpnBxn0MnHcWuW4j2YZyOcDCrj1yMnH9ZX6bggqswNg6lCenX / APJ6HU6WDKMrDJGVIIyDgjbzBBkbNIp6qD8xIseGXdn3JJA / 2Ju8brsTT0gE5ZGWwncsRgdTv0nquo4OpGAAB5YEpeIdm2YKjY5VJII9 / LEtR5n2ZuK2GvlJNhUDbzXOx + EY + 1OoR + WvqULc2xGDsPv1nZNwFanVwMcrA5 + s57jnA2e6wqDgsT995aNng6juKt9wi + xHkN5zXaRHu1uGZiCaq + YnmIGFGcn0z + kvuF6Cyqi / c5UKyA7gfEW2PyE5o3OLRYwJIdWPlnlIOP0gd8 + g4Zaqpdoe7IAXvNM5VtgACyn4jsOuczzPQcMbUagUUEeOxlTnOAFycFm9gNzid3bxNVrFjbB1JTIwScbbee5nPdkqE75jjA7tuu4 + JZJ9DX452J1 + kDPdp27tRk2J + 8rA8ySu4H nkgSgrvI6H6dR9p6Rx7V2po7VSxwrqEKBjynmYAjlO3TMp + wuiozZZqtMt647sKx5SvwsXU4 + LoB08 / WXuDlV1XqPscTMtynz + 4X / 4 / WdP224Nw6upbdIb63ZwhpsIdACGYsH3Pljr5ym4P2R1uqqe7T0mxEbkPKyhs4BOFYjOAR09Yo1OXz8vXyP1hNfV6S2huWxLKm9HVkJx8wMxLqm88H6Y / pAzPPXvwm4V3aNew3P7tf0aw / VuUf4DPJ + EjvLVUA56gepyABn5kT3 / AIJpBVUlYOyqBn1P5j9Tk / WTTHQBo5hqmcSKI4QgVhnMfiGLzpCtIc8zotvdgmwUknvCgG5PQbeRM6krIkTvrm4yvjQSpdNw3TvY3LhS1dlVNX8VjuBk + eBkmW3ZfgX7JWwZ + 8utY2XWHq7n09APIS8xFAIxIkxiRULKEZlYqCy55SRuudjg + WRJ8kccCBWafFrqkr7y4DlrIYZHMefOE5R5uSQABvkibeo1CVqWc8oGMk + 5wJpaLWabV + OsizubCMlWHJYBg7MB4gGP3kGLhujsPNqLAO / dcKrHK0p1WrI98FiOp9gswnQWogqrY89zFtRqNgw2HOyj + 2QAqDooA / sgG85IsSCiStWcJX + 70 + m2dlJXndB8AfryJ1Y53YY8mEzabjFgrfUuT3RIFNZUd5YM8qNtg81jEcq + nLnBJxY6qhbFKOoKkYYHoR6H1HtKvh5 / arRqP / ZRZ + zDydt1bUe4xlU9ix / MMNVaJxQDu1tQrY4PhX94BgZY5G / KMgZI8x6ibiOj5CsCRjIBBIz0yPLzlCKrau91BXvb3wlaLnlVObFaA42XJ5nb3PkoE1raDWE0yNzai4my64DdFyO9t / hJ2RFztgdQhmeOLXSnTj0mlqOGKegA + k19PrHN5qqbNdKjvWbxYcgFKQeueU8zEnbKdcmZtJxxHra1lK1AtizOVZVOC4BAPKSDg + YwehEk0Vep4A2H5eUhlKnPvtn9TO e1XZZVHTJnoqXI2MEb + R2PTOCp3B67e0LNOD5SEeUdoOFc1FKqN15xj6 / + JQ6Lg1iWoclfGoPupYZH2ntN / C6ychAD64lVq + AknKgHBBx0zjylzR5b2rud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IoEwrV4pqu5pstILciFuVQSWIGygDqSdvrMHAOHmnToj4LkF7SOjW2EvYflzMce2JZZhA0dXwqt6loHgrU1 + FcActbKwT + 6eUAj0kdXomfUUucd3ULGx5m1gEU / IKbP5pYwkRU6JGOpvvcFVVUorzsOVR3ljj2LPj / AKYmjoNYa9HbrCDzWGy9QCQSp8NCAe6ivb1Yzd7U2HuDUhw97LQuOo7w4dh / dTnb6Tc1PDkdErI8CNWwA2H7shkB9sqNvaUYLOI20V6dGYWWWNXUebzbHNY2R6Krn6Tev42iOK2VuYqWwAG2BA3wff8AQzU1OhL6iq0kclS2YXzNj8oDfRQ4 / wAUw6DTsdRqLXXA / d1V580RSxYfN7GH + GZ3OlzVtiIxwM6IjCOIyBRQMDAWYRRrAcUDFICEJGFMwEBCVEgYjFHIHzR5kRCBPMRiMawoixHHiEY1rA3wPtJxmKAZizCKBIR5iigSJjzIzW4laVqdlOCFJB98e8uZdGW5KyyF + UspJTOMhiCpK ++ CR9ZmzPK9MTdboLbSXd9U / MzEnZEJUAdFAO + BiepiTfkSEcUcD //Ž","用户ID&#34 ;: " 2kdv1goo9d3nb29evcova6oi4p"}

2 个答案:

答案 0 :(得分:2)

您正在尝试在发送请求后立即阅读响应。在尝试阅读之前,您必须等待响应到达。

var pic = new XMLHttpRequest();
pic.open("GET",  api+"getimage?"+"userID="+myUserID, true);
pic.send();
pic.addEventListener("load", imageHandler);

function imageHandler(event) {
    var picObject = JSON.parse(this.responseText);
    console.log(picObject);
}

答案 1 :(得分:-2)

您必须在base64字符串前加"data:image/png;base64,"