GetScript JSON 是使用 jQuery 的一個組件,可以通過它獲取遠程服務器上 JSON 數據文件并將其加載到頁面中。以下是 JSON 數據文件:
{ "firstName": "John", "lastName": "Doe", "age": 25, "address": { "streetAddress": "123 Main St.", "city": "Anytown", "state": "CA", "postalCode": "12345" }, "phoneNumbers": [ { "type": "home", "number": "555-555-1234" }, { "type": "fax", "number": "555-555-5678" } ] }
使用 GetScript JSON 來加載并解析 JSON 數據:
$.getScript("data.json", function(){ console.log("JSON數據已經獲取完畢,可以進行解析操作。"); //解析 JSON 數據 var firstName = data.firstName; var lastName = data.lastName; var age = data.age; var streetAddress = data.address.streetAddress; var city = data.address.city; var state = data.address.state; var postalCode = data.address.postalCode; var homePhone = data.phoneNumbers[0].number; var faxNumber = data.phoneNumbers[1].number; console.log("姓名: "+firstName+" "+lastName); console.log("年齡: "+age); console.log("地址: "+streetAddress+", "+city+", "+state+", "+postalCode); console.log("電話: 家庭電話: "+homePhone); console.log("傳真號碼: "+faxNumber); });
使用 GetScript JSON 可以更加方便地獲取和解析 JSON 數據,這對于前端開發和數據可視化非常有用。
上一篇c json轉換字符串
下一篇python 文件寫多行