如何读取json文件? ExtJS的

时间:2022-10-29 13:37:51

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!

什么是网址?这是一条绝对的道路吗?这甚至会工作吗?帮帮我!

2 个解决方案

#1


3  

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. You could use an absolute path if you wanted to.

不,users.json是一个相对路径。如果您愿意,可以使用绝对路径。

I would make a data folder inside your app folder and use app/data/users.json as the URL.

我会在你的app文件夹中创建一个数据文件夹,并使用app / data / users.json作为URL。

You should also learn Chrome Developer Tools so that you can see the network requests.

您还应该了解Chrome开发者工具,以便查看网络请求。

#2


1  

{
    "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']
});

#1


3  

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. You could use an absolute path if you wanted to.

不,users.json是一个相对路径。如果您愿意,可以使用绝对路径。

I would make a data folder inside your app folder and use app/data/users.json as the URL.

我会在你的app文件夹中创建一个数据文件夹,并使用app / data / users.json作为URL。

You should also learn Chrome Developer Tools so that you can see the network requests.

您还应该了解Chrome开发者工具,以便查看网络请求。

#2


1  

{
    "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']
});