{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }
<form action="form_action.json" method="get">
<p>First name: <input type="text" name="fname" /></p>
<p>Last name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>
var list=JSON.Parse(json);
for(var i=0;i<list.length;i++){
var $li = $('<li title="'+list[i].title+'"><img style="height:'+list[i].height+'px;width:'+list[i].width+'px;" src="'+list[i].image+'"/></li>');
$('ul').append($li);
}
网上一大堆ajax的教程,可以搜一下,慕课网有视频教程,然后再去w3school看下dom替换元素属性,几分钟就搞定了。
基本就是通过data.image,data.height等(data是你ajax用来表示接受的数据的变量名)获取数据,然后用dom技术替换img标签的src和alt的值。
转换成json对象使用jquery进行操作,如一楼那样
外层用 for in 或$.each 里层用for循环