Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html页面的Json数组元素_Javascript_Html_Json - Fatal编程技术网

Javascript html页面的Json数组元素

Javascript html页面的Json数组元素,javascript,html,json,Javascript,Html,Json,我对使用JSON还不熟悉,我正在尝试做一些我确信非常简单的事情,但我已经寻找了一段时间,还没有找到正确的方法。 我有两个文件,一个html页面和一个包含数组的json文件。 JSON文件的内容如下所示: [ { "name": "EXAMPLE1", "description": "this is a sample description"}, { "name": "EXAMPLE2", "description": "This is a second sample descri

我对使用JSON还不熟悉,我正在尝试做一些我确信非常简单的事情,但我已经寻找了一段时间,还没有找到正确的方法。 我有两个文件,一个html页面和一个包含数组的json文件。 JSON文件的内容如下所示:

[
{ "name": "EXAMPLE1",
      "description": "this is a sample description"},
{ "name": "EXAMPLE2",
  "description": "This is a second sample description"}
]
我试图做的是获取数组元素并在html页面上显示它们,其中“name”是标题(h1),“description”是段落(p)

以下是迄今为止我在html文件中的代码,如果我完全误解了如何进行此操作,我不会感到惊讶:

<!DOCTYPE html>
<html>
    <head>
        <title>Json Arrays</title>  
    </head>

    <body>


    </body>
    <script>
    $(document).ready(function(){

$.getJSON("testarray.json", function(data){
$.each(data, function)(){
$("body").append("<h1>"+this['name']+<br>"</h1>""<p>"+this['description']+<br>"</p>");
}
}
}
        </script>
</html>

Json数组
$(文档).ready(函数(){
$.getJSON(“testarray.json”),函数(数据){
$。每个(数据、函数)(){
$(“正文”)。追加(“+this['name']+
”“”+this['description']+

”); } } }

如果您有任何建议,我们将不胜感激。

您必须正确解析它。以下是我调整的代码

<!DOCTYPE html>
<html>
    <head>
        <title>Json Arrays</title>  
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>

    <body>


    </body>
    <script>
    $(document).ready(function(){

            $.getJSON("testarray.json", function(data){
                console.log(data);
                $.each(data, function(i, field){

                    $("body").append("<h1>"+field.name+"<br></h1><p>"+field.description+"<br></p>");
                    });

            })
        });
        </script>
</html>

Json数组
$(文档).ready(函数(){
$.getJSON(“testarray.json”),函数(数据){
控制台日志(数据);
$.each(数据、函数(i、字段){
$(“body”).append(“+field.name+”
“+field.description+”
”); }); }) });

在每次之后,所有内容都将是对象,所以只需使用
字段。name

它不是那样工作的。您需要一个服务器来提供json。您可以在您的机器上设置一个服务器,返回json文件,然后这就可以了。如果您不想设置服务器,您也可以研究一下:我不完全确定您需要什么意思是“一切都将是对象,所以只需使用field.name”您已经厌倦了将值对象作为数组获取..取而代之的是获取对象及其属性我仍然没有在页面上获得任何输出?只需检查,但在主体上附加应该可以正常工作?您必须遵循的步骤列表。1)您是否试图在任何类型的服务器MAMP、LAMP、XAMPP中执行此操作?2)创建
.json
并粘贴json。3) 复制粘贴通过本地主机运行的HTML4)。仍然存在错误检查控制台查找您面临的错误