简体   繁体   English

如何读取json文件? ExtJS的

[英]How is a json file read? extjs

Say that I have a the fields here for a user in a "json" format: 假设我在这里以“json”格式为用户提供了字段:

{
    "users": [
       {
           "id": 1,
           "name": "Ed Spencer",
           "email": "ed@sencha.com"
       },
       {
           "id": 2,
           "name": "Abe Elias",
           "email": "abe@sencha.com"
       }
    ]
}

I assume that the root here is "users". 我假设这里的根是“用户”。 Would I save this text as a users.json file? 我会将此文本另存为users.json文件吗?

And from there, how do I read from this file (in this case, with ext.js) 从那里,我如何从这个文件中读取(在这种情况下,使用ext.js)

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    fields: ['name', 'email']
});

var store = Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

What is the url? 什么是网址? Is it an absolute path? 这是一条绝对的道路吗? Will this even work??? 这甚至会工作吗? Help! 救命!

In an HTML page, a JavaScript file is served relative to the URL of the HTML page, or from an absolute path. 在HTML页面中,相对于HTML页面的URL或绝对路径提供JavaScript文件。

Would I save this text as a users.json file? 我会将此文本另存为users.json文件吗?

You can, sure. 你可以,当然。

What is the url? 什么是网址?

The URL users.json is relative to the URL of the HTML page that is running your application, so if the HTML page was at http://localhost/myapp/index.html , the relative path users.json would be the same as the absolute path http://localhost/myapp/users.json URL users.json是相对于运行应用程序的HTML页面的URL,因此如果HTML页面位于http://localhost/myapp/index.html ,则相对路径users.json将与绝对路径http://localhost/myapp/users.json

Is it an absolute path? 这是一条绝对的道路吗?

No, users.json is a relative path. 不, users.json是一个相对路径。 You could use an absolute path if you wanted to. 如果您愿意,可以使用绝对路径。

I would make a data folder inside your app folder and use app/data/users.json as the URL. 我会在你的app文件夹中创建一个data文件夹,并使用app/data/users.json作为URL。

You should also learn Chrome Developer Tools so that you can see the network requests. 您还应该了解Chrome开发者工具,以便查看网络请求。

{
    "users": [
       {
           "id": 1,
           "name": "Ed Spencer",
           "email": "ed@sencha.com"
       },
       {
           "id": 2,
           "name": "Abe Elias",
           "email": "abe@sencha.com"
       }
    ]
}

in your model's field, there is no id, which comes from json, there is only name and email fields... 在你的模型领域,没有id,来自json,只有名字和电子邮件字段......

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    fields: ['name', 'email']
});

add id field to model, like this 将id字段添加到模型中,就像这样

Ext.define('AM.model.User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name', 'email']
});

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

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