0

I have following code,

    $.ajax({ 
  type: "POST",
  async: false,
  //dataType: "json",
  url: 'index.php',
  data : { lat : latValue, lng : lngValue },
  success: function (data){  
  console.log(data);

  data = JSON.parse(data);
  console.log(Object.keys(data).length);
  for(var k = 0; k <Object.keys(data).length; k++) {

   // zipcode_final[i][k] = ; 
   break;
}   
  }
  });
 //  csvRows.push(zipcode_final);
  }

}); 

See this edited code. How to get number of items ( length ) of data json object? This returns only number of characters. My Josn array looks like below,

[["44641","44730","44669"]]
2
  • Your code is not complete to figure out what's problem Commented Jul 27, 2016 at 3:06
  • The suggests you only loop once. Have you put some logging in to see what is happening? Commented Jul 27, 2016 at 3:06

1 Answer 1

1

I have added some dummy code for you to figure out. Run the snippet. Check the values. I have commented line for zipcode_final as it wasn't defined.

var str = '{"x":4,"y":5,"z":6}';
var data = $.parseJSON(str);
var k = 0;
$.each(data, function(i, item) {
  console.log('k = ', k, '; i = ', i, ';item = ', item);
  //zipcode_final[i][k] = item;
  k++
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

dj_coder please validate, is this what you were asking for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.