網頁的html樹中的script標簽中包含JSON。 我需要提取該JSON對象並將其作為JSON對象進行操作。 請提出建議,我該如何實現。 我正在嘗試以這種方式:
var url = 'https://indooroopillyshopping.com.au/stores/'
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
console.log("sending request to ", url);
//WAIT
xhr.send();
console.log("receive respond from ", url);
var data = xhr.responseText;
var json = data.match(/\[{"id":"836","title":"Acutherapy".*/g); //I need an object window.STORES = [{}]
var parsedJson = JSON.parse(stringJson);//catch an error
我也嘗試這樣做:
var json = data.match(/{"id":"836","title":"Acutherapy".*/g);
var newjson = json[0].substring(0, json[0].length-2);//remove space and square bracket at the end
var stringJson = JSON.stringify(newjson);
var parsedJson = JSON.parse(stringJson)
console.log(parsedJson[0].id)//I cannot access JSON with dot notation as it acts like a string (object but not [Object object])
我需要做的就是將json字符串轉換為json對象,以訪問其鍵和值。