A
A
Andrey Okhotnikov2019-04-17 18:04:32
JavaScript
Andrey Okhotnikov, 2019-04-17 18:04:32

How to get a link to an image by url from JSON?

There is a url like befree.loc/swatches/ajax/media/?product_id=85072
When you click on it, there should be a picture, but in fact I get this json

{"large":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/948dca29020676657918de8c0c4c7381\/upload\/18315\/1831548578_75.jpg","medium":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75.jpg","small":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75.jpg","gallery":{"92701":{"large":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/948dca29020676657918de8c0c4c7381\/upload\/18315\/1831548578_75.jpg","medium":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75.jpg","small":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75.jpg"},"92702":{"large":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_1\/948dca29020676657918de8c0c4c7381\/upload\/18315\/1831548578_75_1.jpg","medium":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_1\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_1.jpg","small":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_1\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_1.jpg"},"92703":{"large":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_2\/948dca29020676657918de8c0c4c7381\/upload\/18315\/1831548578_75_2.jpg","medium":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_2\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_2.jpg","small":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_2\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_2.jpg"},"92704":{"large":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_3\/948dca29020676657918de8c0c4c7381\/upload\/18315\/1831548578_75_3.jpg","medium":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_3\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_3.jpg","small":"http:\/\/befree.loc\/media\/catalog\/product\/cache\/1831548578_75_3\/c687aa7517cf01e65c009f6943c2b1e9\/upload\/18315\/1831548578_75_3.jpg"}}}

Those. I can't substitute this link as src for img. How to use js to access this url and get a link?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanKud, 2019-04-17
@tsepen

const json = 'ваш_json';
const jsonObj = JSON.parse(json);
console.log(jsonObj); /* объект со всеми ссылками на картинки */
/* например */
console.log(jsonObj.medium); /* http://befree.loc/media/catalog/product/cache/1831548578_75/c687aa7517cf01e65c009f6943c2b1e9/upload/18315/1831548578_75.jpg */
/* и так далее */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question