简体   繁体   English

使用 fetch 读取 JSON 格式的文件

[英]read file in JSON using fetch

I have a JSON file returned by a server我有一个服务器返回的 JSON 文件

{
    "myList": [
    {
        "film": "A"
    }, 
    {
        "film": "B"
    }, 
    {
        "film": "C"
    }]
}

And I want to display them with in React native.我想在 React Native 中显示它们。

  componentDidMount() {
     this.askListing();
  }

  analyse=(responseJson)=>{
     console.log(myList);
  }


  askListing=()=>{
     var getRequest = "https://--------------");
     fetch(getRequest)
        .then((response) => response.json())
        .then((responseJson) => {this.analyse(responseJson)} ) 
        .catch(
          (error) => { console.error(error) }
        );
  }

But in my console I have :但在我的控制台中,我有:

Array [
  Object {
     "film": "A",
  },
  Object {
     "film": "B",
  },
  Object {
     "film": "C",
  },
]
  1. Do you know why "Array" and "Objects" are added?你知道为什么要添加“数组”和“对象”吗?
  2. I want to access films name.我想访问电影名称。 How can I access them ?我怎样才能访问它们? If I do console.log(myList.film) it is undefined (probably because of Array and Objects words but I also have several "film" keyword, isn't it a problem?如果我执行 console.log(myList.film) 它是未定义的(可能是因为 Array 和 Objects 词,但我也有几个“film”关键字,这不是问题吗?

Array and Object are the type of object of class from which it is created. Array 和 Object 是创建它的类的对象类型。

The JavaScript Array object is a global object that is used in the construction of arrays JavaScript Array 对象是一个全局对象,用于构建数组

The Object constructor creates the key value pairs Object 构造函数创建键值对

You need to iterate the list in order to access the films您需要迭代列表才能访问电影

for(var i=0; i < myList.length; i++)
    console.log(myList[i].film);

myList.film returns undefined because film is not a property of the list and moreover an object or a json contains a property that could be accessed using the dot operator. myList.film返回undefined因为film不是列表的属性,而且对象或 json 包含可以使用点运算符访问的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM