读取多个数组内容的JSON文件

时间:2014-11-01 15:35:48

标签: jquery arrays json

我有以下JSON文件json file,我需要阅读内容(客户端请求是使用jQuery) - 我的问题:文件中有多个数组 - 是否只能读取特定数组?

或者需要阅读所有,然后我可以逐个处理? (这也没关系) - 直到现在我只和一个人一起工作#34;简单"文件中的数组,但是这个数组有多个数组我没有经验。

请告诉我解析此文件内容的最佳模式/方法。 要么 其他方法可以起作用。

可以从文件中只读取一个数组吗? -

谢谢!

1 个答案:

答案 0 :(得分:0)

您的JSON文件不包含真正的json字符串...您需要按如下方式更改文件

检查demo

JSON文件(temp.json)

{

    "packages": [
        "Core",
        "Comms",
        "Standards",
        "Content",
        "Events",
        "Advertising",
        "Training",
        "Personal development",
        "Opportunity matching",
        "Deployment",
        "Affiliates",
        "Verification"
    ],
    "bundles": {
        "Just Checks": [ ],
        "Compliance": [
            "Core",
            "Standards"
        ],
        "Comply & Engage": [
            "Core",
            "Comms",
            "Standards",
            "Events"
        ],
        "Full Staff Bank": [
            "Core",
            "Comms",
            "Standards",
            "Events",
            "Opportunity matching",
            "Deployment"
        ]
    },
    "pricing": {
        "packages": {
            "Core": {
                "25": "25.00",
                "100": "38.58",
                "300": "69.44",
                "1,000": "125.00",
                "2,000": "200.00",
                "5,000": "320.00",
                "10,000": "416.00",
                "50,000": "540.80",
                "50000+": "703.04"
            },
            "Comms": {
                "25": "13.33",
                "100": "20.58",
                "300": "37.04",
                "1,000": "66.67",
                "2,000": "106.67",
                "5,000": "170.67",
                "10,000": "221.87",
                "50,000": "288.43",
                "50000+": "374.95"
            },
            "Standards": {
                "25": "10.00",
                "100": "15.43",
                "300": "27.78",
                "1,000": "50.00",
                "2,000": "80.00",
                "5,000": "128.00",
                "10,000": "166.40",
                "50,000": "216.32",
                "50000+": "281.22"
            },
            "Content": {
                "25": "6.67",
                "100": "10.29",
                "300": "18.52",
                "1,000": "33.33",
                "2,000": "53.33",
                "5,000": "85.33",
                "10,000": "110.93",
                "50,000": "144.21",
                "50000+": "187.48"
            },
            "Events": {
                "25": "6.67",
                "100": "10.29",
                "300": "18.52",
                "1,000": "33.33",
                "2,000": "53.33",
                "5,000": "85.33",
                "10,000": "110.93",
                "50,000": "144.21",
                "50000+": "187.48"
            },
            "Advertising": {
                "25": "13.33",
                "100": "20.58",
                "300": "37.04",
                "1,000": "66.67",
                "2,000": "106.67",
                "5,000": "170.67",
                "10,000": "221.87",
                "50,000": "288.43",
                "50000+": "374.95"
            },
            "Training": {
                "25": "2.50",
                "100": "3.86",
                "300": "6.94",
                "1,000": "12.50",
                "2,000": "20.00",
                "5,000": "32.00",
                "10,000": "41.60",
                "50,000": "54.08",
                "50000+": "70.30"
            },
            "Personal development": {
                "25": "13.33",
                "100": "20.58",
                "300": "37.04",
                "1,000": "66.67",
                "2,000": "106.67",
                "5,000": "170.67",
                "10,000": "221.87",
                "50,000": "288.43",
                "50000+": "374.95"
            },
            "Opportunity matching": {
                "25": "13.33",
                "100": "20.58",
                "300": "37.04",
                "1,000": "66.67",
                "2,000": "106.67",
                "5,000": "170.67",
                "10,000": "221.87",
                "50,000": "288.43",
                "50000+": "374.95"
            },
            "Deployment": {
                "25": "13.33",
                "100": "20.58",
                "300": "37.04",
                "1,000": "66.67",
                "2,000": "106.67",
                "5,000": "170.67",
                "10,000": "221.87",
                "50,000": "288.43",
                "50000+": "374.95"
            },
            "Affiliates": {
                "25": "30.00",
                "100": "46.30",
                "300": "83.33",
                "1,000": "150.00",
                "2,000": "240.00",
                "5,000": "384.00",
                "10,000": "499.20",
                "50,000": "648.96",
                "50000+": "843.65"
            },
            "Verification": {
                "25": "25.00",
                "100": "38.58",
                "300": "69.44",
                "1,000": "125.00",
                "2,000": "200.00",
                "5,000": "320.00",
                "10,000": "416.00",
                "50,000": "540.80",
                "50000+": "703.04"
            }
        },
        "checks": {
            "DBS Standard Disclosure": {
                "Just Checks": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Compliance": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Comply & Engage": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Full Staff Bank": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                }
            },
            "DBS Enhanced Disclosure": {
                "Just Checks": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Compliance": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Comply & Engage": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Full Staff Bank": {
                    "25": "10",
                    "100": "10",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "7.50",
                    "5,000": "6.50",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                }
            },
            "Address & ID Check": {
                "Just Checks": {
                    "25": "6",
                    "100": "6",
                    "300": "6",
                    "1,000": "5",
                    "2,000": "5",
                    "5,000": "5",
                    "10,000": "3",
                    "50,000": "3",
                    "50000+": "3"
                },
                "Compliance": {
                    "25": "6",
                    "100": "6",
                    "300": "6",
                    "1,000": "5",
                    "2,000": "5",
                    "5,000": "5",
                    "10,000": "3",
                    "50,000": "3",
                    "50000+": "3"
                },
                "Comply & Engage": {
                    "25": "6",
                    "100": "6",
                    "300": "6",
                    "1,000": "5",
                    "2,000": "5",
                    "5,000": "5",
                    "10,000": "3",
                    "50,000": "3",
                    "50000+": "3"
                },
                "Full Staff Bank": {
                    "25": "6",
                    "100": "6",
                    "300": "6",
                    "1,000": "5",
                    "2,000": "5",
                    "5,000": "5",
                    "10,000": "3",
                    "50,000": "3",
                    "50000+": "3"
                }
            },
            "Right to Work": {
                "Just Checks": {
                    "25": "9",
                    "100": "9",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "8",
                    "5,000": "7",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Compliance": {
                    "25": "9",
                    "100": "9",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "8",
                    "5,000": "7",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Comply & Engage": {
                    "25": "9",
                    "100": "9",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "8",
                    "5,000": "7",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                },
                "Full Staff Bank": {
                    "25": "9",
                    "100": "9",
                    "300": "9",
                    "1,000": "8",
                    "2,000": "8",
                    "5,000": "7",
                    "10,000": "6",
                    "50,000": "6",
                    "50000+": "6"
                }
            }
        }
    }

}

Jquery Ajax

$("button").click(function(){
    $.ajax({url:"temp.json",success:function(result){
      console.log(result.pricing.packages);
    }});
});

<强> HTML

<button>Get Price Packages</button>