gpt4 book ai didi

javascript - 高效地将JSON数据解析到前端列表中

转载 作者:行者123 更新时间:2023-12-03 12:03:01 37 4
gpt4 key购买 nike

我过去两天一直在浏览,似乎无法在任何地方找到这个问题的答案。

我使用 PHP 将所有 SQL 数据解析为 JSON 格式,这是 JSON 的示例:

[
{
"id": "1",
"name": "Jim",
"age": "39",
"address": "12 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "1",
"canWorkNights": "1",
"isStudent": "1"
},
{
"id": "2",
"name": "Fred",
"age": "29",
"address": "13 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "1",
"canWorkNights": "1",
"isStudent": "0"
},
{
"id": "3",
"name": "Bill",
"age": "19",
"address": "14 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "1",
"canWorkNights": "0",
"isStudent": "0"
},
{
"id": "4",
"name": "Tom",
"age": "39",
"address": "15 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "0",
"canWorkNights": "0",
"isStudent": "0"
},
{
"id": "5",
"name": "Cathy",
"age": "29",
"address": "16 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "0",
"canWorkNights": "0",
"isStudent": "1"
},
{
"id": "6",
"name": "Petra",
"age": "19",
"address": "17 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "0",
"canWorkNights": "1",
"isStudent": "0"
},
{
"id": "7",
"name": "Heide",
"age": "39",
"address": "18 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "1",
"canWorkNights": "0",
"isStudent": "0"
},
{
"id": "8",
"name": "William",
"age": "29",
"address": "19 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "1",
"canWorkNights": "0",
"isStudent": "1"
},
{
"id": "9",
"name": "Ted",
"age": "19",
"address": "20 High Street, London",
"hasCar": "0",
"speaksForeignLanguage": "0",
"canWorkNights": "0",
"isStudent": "1"
},
{
"id": "10",
"name": "Mike",
"age": "19",
"address": "21 High Street, London",
"hasCar": "1",
"speaksForeignLanguage": "0",
"canWorkNights": "0",
"isStudent": "1"
},
{
"id": "11",
"name": "Jo",
"age": "19",
"address": "22 High Street, London",
"hasCar": "0",
"speaksForeignLanguage": "1",
"canWorkNights": "0",
"isStudent": "1"
}
]

这只是一个示例,并不是我正在使用的实际数据,我的 JSON 数据每行包含 8 个图像图像链接,并包含更多字符串数据。

我需要在前端列出最多 120 条记录,此列表将具有过滤搜索选项。

当客户端访问搜索页面时,我猜测他们需要加载所有 JSON 数据,这可能会导致用户体验缓慢。

是否可以根据请求加载 JSON 数据,以便客户端不需要立即加载所有数据?例如,也许每页只请求 10 条记录的数据,因此不会加载整个 JSON 文件?

任何建议都会很棒。

最佳答案

您可以使用local storage 。应该是这样的:

// your json data
var json = {};

// you have store it as string
localStorage.setItem("data", JSON.stringify(json));

然后在你需要的页面中,这样获取即可:

var data = localStorage.getItem("data");

// if the "data" key isn't set on the storage, it returns null
if (data != null)
{
// now deserialize the string to json
data = JSON.parse(data);
}

本地存储在所有主要浏览器版本中都可用,如您所见 here .

关于javascript - 高效地将JSON数据解析到前端列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325457/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com