如何使用javascript从json文件中读取和打印数据?

时间:2022-09-06 13:13:16

I have multiple json files with format like this:

我有多个json文件,格式如下:

{
    "title" : "abc",
    "desc" : "pqr",
    "img" : "mno",
     "isExpertApproved" : "true"
  }

I just want to add one button in my website where I can upload the file and without storing it anywhere I want to read the data of that file and I also want to store that data into database using javascript.

我只想在我的网站上添加一个按钮,我可以上传文件而不将其存储在我想要读取该文件数据的任何地方,我也想用javascript将数据存储到数据库中。

Is there any way for this? Thank you in advance.

这有什么办法吗?先谢谢你。

1 个解决方案

#1


1  

index.html

<div class='wrapper'>
    <p>JSON will be received in 3 seconds</p>
    <ul id='post'></ul>
</div>

external.js

new Request.JSON({
    url: '/echo/json/',
    data: {
        json: JSON.encode({
            text: 'some text',
            array: [1, 2, 'three'],
            object: {
                par1: 'another text',
                par2: [3, 2, 'one'],
                par3: {}
            }
        }),
        delay: 3
    },
    onSuccess: function(response) {
        show_response(response, $('post'));
    }
}).send();

show_response = function(obj, result) {
    $H(obj).each(function(v, k) {
        new Element('li', {
            text: k + ': ' + v
        }).inject(result);
    });
    result.highlight();
};

css

body {
    font-family: Helvetica, Sans, Arial;
}
p, ul {
    padding: 10px;
}
ul {
    margin: 5px;
    border: 2px dashed #999;
}

Click here demo

点击这里演示

#1


1  

index.html

<div class='wrapper'>
    <p>JSON will be received in 3 seconds</p>
    <ul id='post'></ul>
</div>

external.js

new Request.JSON({
    url: '/echo/json/',
    data: {
        json: JSON.encode({
            text: 'some text',
            array: [1, 2, 'three'],
            object: {
                par1: 'another text',
                par2: [3, 2, 'one'],
                par3: {}
            }
        }),
        delay: 3
    },
    onSuccess: function(response) {
        show_response(response, $('post'));
    }
}).send();

show_response = function(obj, result) {
    $H(obj).each(function(v, k) {
        new Element('li', {
            text: k + ': ' + v
        }).inject(result);
    });
    result.highlight();
};

css

body {
    font-family: Helvetica, Sans, Arial;
}
p, ul {
    padding: 10px;
}
ul {
    margin: 5px;
    border: 2px dashed #999;
}

Click here demo

点击这里演示