0

My ajax result returns this Json object below , I would like to turn this into a javascript associative array to loop through it ..

{"store_id":"10","company_id":"1","username":"tony","password":"19f060ed6b09502715175c1cdb562df8eec1070c","name":"Dbarpos1","email":"dfbarco@gmail.com","address":"2738 N Bristol St Santa Ana CA 92706 ","phone_number":"7142425639","city":"","state":"","zip":"","goals":"New 40, Up 35, Acc $1,500","return_policy":" 0 returms","drawer_drop":"after_300","drawer_option":"indiv_200","sales_tax":"8.5%","store_schedule":"","store_logo":"","group":"","created_by":"diego barco","status":"active","contact_person":"Diego Castillo"}

  • javascript has objects, not associative arrays. – Sam Dufel Jun 03 '14 at 20:37
  • That is JSON data, which is already an "associative array" (also known as a hash, dictionary). You can index properties in JavaScript using dictionary notation. For example, `foo.bar` can also be invoked as `foo["bar"]` – Mario J Vargas Jun 03 '14 at 20:39
  • Not sure why this was closed as a duplicate of how to parse JSON. The user is asking how to access the JSON object as an associative array, not how to parse the object. – villecoder Jun 03 '14 at 20:43
  • Wait, you want to loop through the values shown above, or you want an array of objects that you can loop through? – Sean Kendle Jun 03 '14 at 20:48
  • loop through it as an associative array – user3704664 Jun 03 '14 at 20:58

2 Answers2

1

Update your ajax call to include datatype:

$.ajax({
  url:"edit_store_ajax.php", 
  data:{store: store_id}, 
  type:"POST", 
  success:function(result){ for (var key in result) { } },
  dataType:"json"
});

If datatype doesn't help, you will need to parse the String into a JSON Object. You can use jQuery.parseJSON() to help: http://api.jquery.com/jquery.parsejson/

$.ajax({
  url:"edit_store_ajax.php", 
  data:{store: store_id}, 
  type:"POST", 
  success:function(result){ result = $.parseJSON(result); for (var key in result) { } }
});

You can loop through it in the same way you would an associative array...

for (var key in json) {
  console.log(key + ":" + json[key]);
}

Or to get single values:

console.log(json["store_id"]);
console.log(json["username"]);
console.log(json.store_id);
console.log(json.username);
mothmonsterman
  • 2,451
  • 3
  • 21
  • 28
  • That doesnt work the way it should .. it returns every single character in the loop – user3704664 Jun 03 '14 at 20:42
  • @user3704664 Did you parse the JSON string into a JSON object before using this code? If not, I'll have to rethink my comment on the question. – villecoder Jun 03 '14 at 20:45
  • if his "ajax response returned the JSON object" why would it need to be parsed? obviously a String would need to be parsed but that is not what OP said... – mothmonsterman Jun 03 '14 at 20:46
  • @happytimeharry OP's comment suggest that the response is not a JSON object but a string that needs to be thrown through JSON.parse() – villecoder Jun 03 '14 at 20:48
  • @villecoder sorry, what suggests that? the FIRST SENTENCE that says "My ajax result returns this Json object below" or the fact that it is not wrapped with quotes in the code snippet?.. plus it is tagged JQuery, which parses it for you automatically... – mothmonsterman Jun 03 '14 at 20:50
  • It's not working bro ... it returns every single character – user3704664 Jun 03 '14 at 20:54
  • Below is the ajax code $(document).ready(function() { $(".select_store").click(function() { var store_id = $(this).val(); $.ajax({ url:"edit_store_ajax.php", data : {store: store_id}, type : "POST", success:function(result){ for (var key in result) { } } }); }); }); – user3704664 Jun 03 '14 at 20:55
  • see the edit, use dataType:"json" to coerce it for you – mothmonsterman Jun 03 '14 at 21:02
  • it doesnt make any sense... a Fiddle would help – user3704664 Jun 03 '14 at 21:05
  • @happytimeharry Trust me when I say that your code works as advertised and that OP is very likely acting on a string. See http://jsfiddle.net/7z4hh/. First set of code acts on a string. Second set acts on the same "object" after it has been JSON parsed. – villecoder Jun 03 '14 at 21:06
  • @villecoder 10-4 buddy i just took the OP literally for what was said and showed... def a string at this point – mothmonsterman Jun 04 '14 at 15:26
  • what doesnt make sense? just add the dataType:"json" to your ajax options and see if it makes a difference. if it doesnt you can use jquery to parse the json as suggested by @villecoder... $.parseJSON(string) http://api.jquery.com/jquery.parsejson/ – mothmonsterman Jun 04 '14 at 15:31
0

You can use JSON.parse(data) in more recent browsers, and a polyfill if you support older ones.

Razor
  • 27,418
  • 8
  • 53
  • 76