admin管理员组文章数量:1303054
I am calling a function this way .
var responseinner = returnvalues(selectedeleemnt);
console.log(responseinner);
displaying as Object
console.log(JSON.stringify(responseinner));
displaying as [{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
I tried all the ways of parsing this value
[{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
But always it throws the error as
Uncaught TypeError: Cannot read property 'length' of undefined
I used JSON.parse , JSON.stringfy() . but none helped .
for (var i = 0; i < responseinner.type.length; i++) {
}
could anybody please help me
I am calling a function this way .
var responseinner = returnvalues(selectedeleemnt);
console.log(responseinner);
displaying as Object
console.log(JSON.stringify(responseinner));
displaying as [{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
I tried all the ways of parsing this value
[{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
But always it throws the error as
Uncaught TypeError: Cannot read property 'length' of undefined
I used JSON.parse , JSON.stringfy() . but none helped .
for (var i = 0; i < responseinner.type.length; i++) {
}
could anybody please help me
Share Improve this question edited May 29, 2014 at 14:40 Pointy 414k62 gold badges594 silver badges627 bronze badges asked May 29, 2014 at 14:22 user663724user663724 2- Why is JSON involved at all? – Pointy Commented May 29, 2014 at 14:25
-
You need an index;
responseinner[0].type.length
to identify the object containing thetype
array you wish to examine. – Alex K. Commented May 29, 2014 at 14:25
6 Answers
Reset to default 3for(var i=0;i<responseinner.length;i++){
responseinner[i].type .... }
Your responseinner
isn't an Object
but an Array
with only one element.
So, you'll have to use responseinner[0].type.length
If it was an object, it would have started with {}
and not []
, with what arrays start.
You can use of
for (let element of responseinner) {
// do something../
console.log(element.name)
}
To parse json with jquery:
$.each($.parseJSON(responseinner), function(key,value){
//do something
});
If responseinner is already formed as a json you don't need the $.parseJSON
or
responseineer[0].name
responseineer[0].image
...
Json data es into an array so get it's object with an index, so to access it: responseineer[0]
try test[0].type.length
you have an Object in an array of length 1 therefore you must first define the index location of the Object in the array which is trivial since the array has 1 defined index.
for (var i = 0; i < responseinner[0].type.length; i++) {
}
for(var i=0;i
Its works fine but...how to assign to gridview in client side
本文标签:
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'length' of undefined for simple JSON array - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741724620a2394561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论