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;
});

更多相关文章

  1. PHP面向对象笔记 —— 113 封装概念
  2. 关于静态方法不能调用类中的非静态属性的理解
  3. PHP的语言特性-面向对象和C++/java/python的相似之处
  4. php面向对象之抽象类和接口理解
  5. 键入提示 - 指定对象数组
  6. PHP 创建对象的两种方法
  7. Python:我如何从datetime.timedelta对象中获取时间?
  8. 通过PHP设置html属性的最佳做法是什么?
  9. php如何以一个对象作为数组下标?

随机推荐

  1. php有必要转前端吗
  2. 如何巧用 PHP 数组函数
  3. PHP简短而安全的数组遍历
  4. php怎么把文件设置为插件
  5. php开启和关闭错误提示的方法介绍
  6. PHP生成器-动态生成内容的数组
  7. PHP7中创建COOKIE和销毁COOKIE的方法
  8. PHP中设置session过期的方法
  9. php实现将文件上传到临时目录
  10. PHP7中创建session和销毁session的方法