简体   繁体   中英

fetch json value with no key using jquery

i am having trouble fetch JSON using jQuery, if possible someone tell me what mistake i made because my code is not working

data.json file contain

 { "value1", "value2", "value3", "value4" } 

and here is my jquery code

 $.getJSON( "data.json", function( data ) { $.each( data, function( val ) { alert(val); }); }); 

use [] instead of {} in the json file as suggested in the comment as your current json is not a valid JSON. Then you can access the file data this way.

$.each(data, function(i,val) {
    alert(val);
  });

Notice, that your current code only access the index of the value but not the actual value. Place the both parameter i and val to get both index and value from the file.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM