如何加载json文件?
16lz
2021-01-22
How could I get access to json from another file?
我怎样才能从另一个文件访问json?
I tried:
我试过了:
obdatabase.json
obdatabase.json
{ "pobject" :[
{ "pname":"Pikachu" , "pid":"1" },
{ "pname":"squirtle" , "pid":"2" },
{ "pname":"Justinbieber" , "pid":"3" }
]};
test.php
test.php的
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="obdatabase.json"></script>
<script>
$(function() {
console.log(pobject);
});
</script>
1 个解决方案
#1
2
Use jQuery's getJSON()
method:
使用jQuery的getJSON()方法:
var obj = $.getJSON( 'obdatabase.json' );
Since getJSON
will automatically parse the result into a Javascript object, you may access the properties as follows:
由于getJSON会自动将结果解析为Javascript对象,因此您可以按如下方式访问属性:
// Get the object:
var db;
$.getJSON( 'obdatabase.json', function(obj) {
// Now we can access properties:
obj.pobject[0].pname; // Will be Pikachu
// Assign obj to db (so we can access it outside of the callback):
db = obj;
});
更多相关文章
- PHP面向对象笔记 —— 113 封装概念
- 关于静态方法不能调用类中的非静态属性的理解
- PHP的语言特性-面向对象和C++/java/python的相似之处
- php面向对象之抽象类和接口理解
- 键入提示 - 指定对象数组
- PHP 创建对象的两种方法
- Python:我如何从datetime.timedelta对象中获取时间?
- 通过PHP设置html属性的最佳做法是什么?
- php如何以一个对象作为数组下标?