How to read an external local JSON file in JavaScript?

前端 未结 22 2291
醉酒成梦
醉酒成梦 2020-11-22 02:53

I have saved a JSON file in my local system and created a JavaScript file in order to read the JSON file and print data out. Here is the JSON file:

{"res         


        
22条回答
  •  耶瑟儿~
    2020-11-22 02:59

    Just use $.getJSON and then $.each to iterate the pair Key /value. Content example for the JSON file and functional code:

        {
            {
                "key": "INFO",
                "value": "This is an example."
            }
        }
    
        var url = "file.json";         
        $.getJSON(url, function (data) {
            $.each(data, function (key, model) {
                if (model.key == "INFO") {
                    console.log(model.value)
                }
            })
        });
    

提交回复
热议问题