Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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/4/json/15.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/1/angularjs/24.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中获取json值?_Javascript_Json - Fatal编程技术网

如何在javascript中获取json值?

如何在javascript中获取json值?,javascript,json,Javascript,Json,我使用javascript动态创建了图像标记,并使用了一个JSON文件。图像标记单击调用的onclickfunctiongetModefunction。此函数参数id只有我可以在参数中使用。但是我不能使用id和objMaterial两个参数。我需要JSON objMaterial值也如何获得 function floorGridToolList(jsonPath, parentID){ document.getElementById('floorTexture').st

我使用javascript动态创建了图像标记,并使用了一个JSON文件。图像标记单击调用的
onclick
function
getMode
function。此函数参数
id
只有我可以在参数中使用。但是我不能使用id和objMaterial两个参数。我需要JSON objMaterial值也如何获得

function floorGridToolList(jsonPath, parentID){

            document.getElementById('floorTexture').style.display = 'block';

            var path = $.getJSON(jsonPath, function (jsonObjectList) {

              for (i = 0; i < jsonObjectList.objects.length; i++) {

                var imgTag = document.createElement('img');
                imgTag.setAttribute('src', jsonObjectList.objects[i].iconImage);
                imgTag.setAttribute('id', jsonObjectList.objects[i].id);
                imgTag.setAttribute("title", jsonObjectList.objects[i].iconTitle);

                imgTag.onclick = function () {
        //getMode(this.id);  It's working
        getMode(this.id, jsonObjectList.objects[i].objMaterial); //Error                    };
                parentID.appendChild(imgTag);
                }

         });

    }
“错误”

未捕获的TypeError:无法读取HTMLImageElement.imgTag.onclick(objectDraw.js:1219)处未定义的属性“objMaterial” imgTag.onclick@objectDraw.js:1219

getMode(this.id, jsonObjectList.objects[i].objMaterial) //try using this

因为在json中,对象是一个带有键0、1、2等的数组,所以没有定义。。您试图指向数组中没有相应值的“id”。因此出现了未定义的错误。

您还必须在图像标记中保存objMaterial,如下所示:

imgTag.setAttribute("objMaterial", jsonObjectList.objects[i].objMaterial);

然后您可以在getMode函数中使用
this.objMaterial
检索它。

您期望的输出是什么?我需要id基json文件objMaterial值如何获取?getMode(this.id,jsonObjectList.objects[id].objMaterial);这种方法是错误的。如何获取基于此.id的objMaterial值@Zabusa请使用正确的格式输入问题的预期输出您正在以getMode(this.id,jsonObjectList.objects[id].objMaterial)的形式将id作为索引传递给对象数组。它应该是objects[i].objMaterialimgTag.setAttribute(“objMaterial”,jsonObjectList.objects[i].objMaterial);接下来是我使用的getMode(this.id,this.objMaterial)。它也是未定义的。@mayank jaincan您可以分享您在这里得到的确切错误。我使用了getMode(this.id,jsonObjectList.objects[I].objMaterial)。运行时我收到此错误“Uncaught TypeError:无法读取HTMLImageElement.imgTag.onclick objectDraw.js:1197)”处未定义的属性“objMaterial”@Rubin Johnya是否尝试从对象数组中获取单个id?jsonObjectList.objects[i].id//在循环中使用时,应返回每个id。
imgTag.setAttribute("objMaterial", jsonObjectList.objects[i].objMaterial);