2017-11-05 11 views
0

현재 이미지의 URL이 필요하며 JSON 파일을 통해 가져옵니다. 처음에는 # 키를 가지고 있기 때문에 URL이있는 키를 가져올 수 없습니다. 여기에 JSON이 있습니다 :Json 키에 '#'이 있습니다.

{ 
"#text":"https://lastfm-img2.akamaized.net/i/u/34s/3ed37777196a6f2c29c02a1a58a93e4d.png", 
"size":"small" 
}, 
{ 
"#text":"https://lastfm-img2.akamaized.net/i/u/64s/3ed37777196a6f2c29c02a1a58a93e4d.png", 
"size":"medium" 
} 
+4

'JSON [ '# 텍스트']'? – Flying

+0

'# text' 대신에 무엇을 기대하십니까? – edkeveked

+0

문자열이 – user93

답변

2

다른 JSON 문자열을 발견 할 때마다 마찬가지입니다! >«array»[index]['#text']-«object»[property] :

#는 속성 이름에 잘못된 문자, 해결책은 브래킷 표기법입니다.

을 사용하여 결과를 추출 할 수 있습니다.

var string = '[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/3ed37777196a6f2c29c02a1a58a93e4d.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/3ed37777196a6f2c29c02a1a58a93e4d.png","size":"medium"}]'; 
 

 
var parsed = JSON.parse(string); 
 

 
//parsed is an array, we can loop over it 
 
parsed.forEach(function(obj) { 
 
     console.log(obj['#text']); 
 
});

당신은 크기에 따라 배열에서 선택할 수있는 경우 것조차 예뻐 :

var string = '[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/3ed37777196a6f2c29c02a1a58a93e4d.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/3ed37777196a6f2c29c02a1a58a93e4d.png","size":"medium"}]'; 
 

 
function getImageUrlBySize(size, json) { 
 
    var parsed = JSON.parse(json); 
 
    return parsed.find(function(element) { //modern browsers only (no IE) 
 
     return element['size'] == size; 
 
    })['#text']; //add text here since find returns the full item 
 
} 
 

 
console.log(getImageUrlBySize('small', string));

+0

나는 마지막으로 fm api도 사용하고 있습니다. 현재 나는 반복되는 몇 곡의 노래를 가지고 있으며 이전에 대상에서 언급 한 문자열을 어떻게 얻을 수 있습니까? – jhigg15