Как спарсить json?

Поддержка
  • {"response":{"count":501,"items":[{"id":306929450,"album_id":176434174,"owner_id":-2322,"user_id":10,"sizes":[{"type":"s","url":"https://via.com/WEkd/W_nh26LTFUY.jpg","width":75,"height":51},{"type":"m","url":"https://via.com/WEkd/Wdfg.jpg

    Подскажите, пожалуйста, как в данном ответе спарсить url, но только от type с указателем m?

  • @ishka94 said in Как спарсить json?:

    {"response":{"count":501,"items":[{"id":306929450,"album_id":176434174,"owner_id":-2322,"user_id":10,"sizes":[{"type":"s","url":"https://via.com/WEkd/W_nh26LTFUY.jpg","width":75,"height":51},{"type":"m","url":"https://via.com/WEkd/Wdfg.jpg

    Подскажите, пожалуйста, как в данном ответе спарсить url, но только от type с указателем m?

    Выложите полностью json. Можете ещё воспользоваться модулем

  • @fox

    {"response":{"count":501,"items":[{"id":306929450,"album_id":176434174,"owner_id":-29771322,"user_id":100,"sizes":[{"type":"s","url":"https:\/\/pp.userapi.com\/WEkdPu2mtsT_o_8G5v4IUKX08rq9LF17t56nsg\/W_nh26LTFUY.jpg","width":75,"height":51},{"type":"m","url":"https:\/\/pp.userapi.com\/7PP175ZkIkEOoEpJf6pxU6478OwfY2ocX9h5FA\/KbDVSBln0BE.jpg","width":130,"height":88},{"type":"x","url":"https:\/\/pp.userapi.com\/29EpKIO7EtV0-UxrIZyWkVAJR0WwnyRJk99AWQ\/pNdUrFGzPQI.jpg","width":604,"height":410},{"type":"y","url":"https:\/\/pp.userapi.com\/6LeNEyazP72EfYp9HXLAnqzZzXz9yrwc2Khbkw\/c1LmQ6jdxIM.jpg","width":660,"height":448},{"type":"o","url":"https:\/\/pp.userapi.com\/7PP175ZkIkEOoEpJf6pxU6478OwfY2ocX9h5FA\/KbDVSBln0BE.jpg","width":130,"height":88},{"type":"p","url":"https:\/\/pp.userapi.com\/EO10ImqfASd7o81fMJLCfZx7olPA_hWqp9l7og\/TAx5J45dmc8.jpg","width":200,"height":136},{"type":"q","url":"https:\/\/pp.userapi.com\/izGti1hbFSAwpW8tA5bsK_ChVRMJvrA2BvNKvw\/s5lO3FE2UWo.jpg","width":320,"height":217},{"type":"r","url":"https:\/\/pp.userapi.com\/nIxFlxqDweZuavMLoLaNfE3gIQgu2tkmSeoz-g\/oAxbp7oYAy4.jpg","width":510,"height":346}],"text":"","date":1373241070},{"id":306929451,"album_id":176434174,"owner_id":-29771322,"user_id":100,"sizes":[{"type":"s","url":"https:\/\/pp.userapi.com\/ZiXPcPyhwSdBMXNRY64UU4Nm2YAmq_KnTRtKNQ\/RL8xeNx0Pv0.jpg","width":75,"height":53},{"type":"m","url":"https:\/\/pp.userapi.com\/LxQlS_0FrPq7aTYcvyBl1CChXetFOlNwkneMgg\/NwQX1reVSvU.jpg","width":130,"height":92},{"type":"x","url":"https:\/\/pp.userapi.com\/RqPOgUj4Jmhutc26J8R180bjxVJ7chFx9udcnQ\/ep5FLhO4Gn0.jpg","width":604,"height":428},{"type":"y","url":"https:\/\/pp.userapi.com\/5dGRNv2QwTH8V28teXZUZbTgy7CRZYzR0Pft9w\/ybFX7zezWPM.jpg","width":807,"height":572},{"type":"z","url":"https:\/\/pp.userapi.com\/fhSXloxGo_qrbQrScjLEXX90-S2nJQgB2eiFPw\/pjmcumostug.jpg","width":1024,"height":726},{"type":"o","url":"https:\/\/pp.userapi.com\/LxQlS_0FrPq7aTYcvyBl1CChXetFOlNwkneMgg\/NwQX1reVSvU.jpg","width":130,"height":92},{"type":"p","url":"https:\/\/pp.userapi.com\/o4s5em5V3pj9Of4gL8l-InuN-Bpw0Jrjic7y6A\/zo6Yrw_jW4Y.jpg","width":200,"height":142},{"type":"q","url":"https:\/\/pp.userapi.com\/3FMW1a_ZDiIEsp510mKitf39pcb6yjwtMj0tSA\/ruR9VDDLhYg.jpg","width":320,"height":227},{"type":"r","url":"https:\/\/pp.userapi.com\/87pqOfTMlDk0tC5exHHSDurcBGKrBMWvtk9VUA\/m4ahSn47FFQ.jpg","width":510,"height":362}],"text":"","date":1373241072}]}}
    
  • @ishka94 said in Как спарсить json?:

    @fox

    {"response":{"count":501,"items":[{"id":306929450,"album_id":176434174,"owner_id":-29771322,"user_id":100,"sizes":[{"type":"s","url":"https:\/\/pp.userapi.com\/WEkdPu2mtsT_o_8G5v4IUKX08rq9LF17t56nsg\/W_nh26LTFUY.jpg","width":75,"height":51},{"type":"m","url":"https:\/\/pp.userapi.com\/7PP175ZkIkEOoEpJf6pxU6478OwfY2ocX9h5FA\/KbDVSBln0BE.jpg","width":130,"height":88},{"type":"x","url":"https:\/\/pp.userapi.com\/29EpKIO7EtV0-UxrIZyWkVAJR0WwnyRJk99AWQ\/pNdUrFGzPQI.jpg","width":604,"height":410},{"type":"y","url":"https:\/\/pp.userapi.com\/6LeNEyazP72EfYp9HXLAnqzZzXz9yrwc2Khbkw\/c1LmQ6jdxIM.jpg","width":660,"height":448},{"type":"o","url":"https:\/\/pp.userapi.com\/7PP175ZkIkEOoEpJf6pxU6478OwfY2ocX9h5FA\/KbDVSBln0BE.jpg","width":130,"height":88},{"type":"p","url":"https:\/\/pp.userapi.com\/EO10ImqfASd7o81fMJLCfZx7olPA_hWqp9l7og\/TAx5J45dmc8.jpg","width":200,"height":136},{"type":"q","url":"https:\/\/pp.userapi.com\/izGti1hbFSAwpW8tA5bsK_ChVRMJvrA2BvNKvw\/s5lO3FE2UWo.jpg","width":320,"height":217},{"type":"r","url":"https:\/\/pp.userapi.com\/nIxFlxqDweZuavMLoLaNfE3gIQgu2tkmSeoz-g\/oAxbp7oYAy4.jpg","width":510,"height":346}],"text":"","date":1373241070},{"id":306929451,"album_id":176434174,"owner_id":-29771322,"user_id":100,"sizes":[{"type":"s","url":"https:\/\/pp.userapi.com\/ZiXPcPyhwSdBMXNRY64UU4Nm2YAmq_KnTRtKNQ\/RL8xeNx0Pv0.jpg","width":75,"height":53},{"type":"m","url":"https:\/\/pp.userapi.com\/LxQlS_0FrPq7aTYcvyBl1CChXetFOlNwkneMgg\/NwQX1reVSvU.jpg","width":130,"height":92},{"type":"x","url":"https:\/\/pp.userapi.com\/RqPOgUj4Jmhutc26J8R180bjxVJ7chFx9udcnQ\/ep5FLhO4Gn0.jpg","width":604,"height":428},{"type":"y","url":"https:\/\/pp.userapi.com\/5dGRNv2QwTH8V28teXZUZbTgy7CRZYzR0Pft9w\/ybFX7zezWPM.jpg","width":807,"height":572},{"type":"z","url":"https:\/\/pp.userapi.com\/fhSXloxGo_qrbQrScjLEXX90-S2nJQgB2eiFPw\/pjmcumostug.jpg","width":1024,"height":726},{"type":"o","url":"https:\/\/pp.userapi.com\/LxQlS_0FrPq7aTYcvyBl1CChXetFOlNwkneMgg\/NwQX1reVSvU.jpg","width":130,"height":92},{"type":"p","url":"https:\/\/pp.userapi.com\/o4s5em5V3pj9Of4gL8l-InuN-Bpw0Jrjic7y6A\/zo6Yrw_jW4Y.jpg","width":200,"height":142},{"type":"q","url":"https:\/\/pp.userapi.com\/3FMW1a_ZDiIEsp510mKitf39pcb6yjwtMj0tSA\/ruR9VDDLhYg.jpg","width":320,"height":227},{"type":"r","url":"https:\/\/pp.userapi.com\/87pqOfTMlDk0tC5exHHSDurcBGKrBMWvtk9VUA\/m4ahSn47FFQ.jpg","width":510,"height":362}],"text":"","date":1373241072}]}}
    

    Можно перебором

    [[ACC]] = []
    var json = JSON.parse([[SAVED_CONTENT]]).response.items
    for (i = 0; i < json.length; i++) {
        for (q = 0; q < json[i].sizes.length; q++) {
            if (json[i].sizes[q].type == "m") {
                [[ACC]].push(json[i].sizes[q].url)
            }
        }
    }
    

    либо через filter()

    Вот тестовый скрипт

  • @fox но ведь type == "m" только один. Не удобнее будет вот так?

    var json = JSON.parse([[SAVED_CONTENT]]).response.items
    for (i = 0; i < json.length; i++) {
        for (q = 0; q < json[i].sizes.length; q++) {
            if (json[i].sizes[q].type == "m") {
                [[ACC]] = json[i].sizes[q].url
            }
        }
    }
    
  • @uraabk said in Как спарсить json?:

    @fox но ведь type == "m" только один. Не удобнее будет вот так?

    var json = JSON.parse([[SAVED_CONTENT]]).response.items
    for (i = 0; i < json.length; i++) {
        for (q = 0; q < json[i].sizes.length; q++) {
            if (json[i].sizes[q].type == "m") {
                [[ACC]] = json[i].sizes[q].url
            }
        }
    }
    

    В массиве items два элемента - объекта,
    0_1557335312861_1345b653-26e1-4988-95a3-838e4c245318-изображение.png
    в каждом есть ещё массив sizes,
    0_1557335231189_ae0d40cf-097e-4e9e-963e-bf832164c4c8-изображение.png
    и вот в нём уже только одна ссылка с типом m.

    Твой код получит ссылку только из последнего элемента массива items.

  • @fox точно, не заметил

  • @fox said in Как спарсить json?:

    Можно перебором

    @uraabk said in Как спарсить json?:

    @fox но ведь type == "m" только один. Не удобнее будет вот так?

    Спасибо обоим!

  • 0 Votes
    3 Posts
    547 Views
  • 0 Votes
    3 Posts
    631 Views
  • Работа с json

    Поддержка
    0 Votes
    3 Posts
    727 Views
  • 0 Votes
    2 Posts
    545 Views
  • 1 Votes
    14 Posts
    3394 Views