user663724
user663724

Reputation:

Uncaught TypeError: Cannot read property 'length' of undefined for simple JSON array

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

Upvotes: 1

Views: 15971

Answers (6)

LEE TAE YONG
LEE TAE YONG

Reputation: 11

You can use of

for (let element of responseinner) {
    // do something../
    console.log(element.name)
}

Upvotes: 1

Vinoth Narayan
Vinoth Narayan

Reputation: 275

for(var i=0;i

Its works fine but...how to assign to gridview in client side

Upvotes: 0

ian
ian

Reputation: 149

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++) {

}

Upvotes: 0

victorvictord
victorvictord

Reputation: 56

for(var i=0;i<responseinner.length;i++){ responseinner[i].type .... }

Upvotes: 3

Amit Joki
Amit Joki

Reputation: 59232

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.

Upvotes: 1

Aboca
Aboca

Reputation: 575

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 comes into an array so get it's object with an index, so to access it: responseineer[0]

Upvotes: 0

Related Questions