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 the type array you wish to examine. – Alex K. Commented May 29, 2014 at 14:25
Add a ment  | 

6 Answers 6

Reset to default 3

for(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

本文标签: