将JSON数据有效地解析到前端列表中

时间:2014-08-15 11:13:43

标签: javascript json

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

我使用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文件没有被加载?

任何建议都会很棒。

1 个答案:

答案 0 :(得分:1)

您可以使用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