在服務器端做我有2個散列我編碼成JSON字符串,像這樣如何解碼JSON字符串?
my $j = JSON->new;
$j = $j->utf8;
my $data;
$data->{users} = $j->encode(\%user_result);
$data->{owners} = $j->encode(\%owner_result);
$json_string = to_json($data);
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;
在客戶端我有
$(document).ready(function(){
$('form').live('submit', function(){
$.ajax({
type: "GET",
url: "/cgi-bin/ajax_confirm.pl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $(this).serialize(),
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('div#create_result').text("responseText: " + XMLHttpRequest.responseText +
", textStatus: " + textStatus +
", errorThrown: " + errorThrown);
$('div#create_result').addClass("error");
},
success: function(result){
if (result.error) {
$('div#create_result').text("result.error: " + result.error);
$('div#create_result').addClass("error");
} else { // perl script says everything is okay
var users = result.users;
var owners = result.owners;
...
users
包含
{"ss":"Sandra Schlichting","fn":"Full name"}
但不是一個數組。當我使用$.each()
時,它需要一次性。
問題
我如何把它變成一個數組,這樣我就可以使用
function makeTable(users) {
var result = '<table>\n<tr><td>Initials</td><td>Full Name</td></tr>\n';
$.each(users, function(index, value) {
result += '<tr><td>' + index + '</td><td>' + value + '</td></tr>\n';
});
result += '</table>';
return (result);
}
應該產生
Initials Full Name
ss Sandra Schlichting
fn Full name
您應該在最後一個問題中爲寫這個jQuery代碼的人留下評論。不開一個新的問題,它只會導致不良的碎片。 – 2011-05-17 12:01:29
可能的重複[如何在HTML中包含數組內容?](http://stackoverflow.com/questions/6028241/how-to-include-array-content-in-html) – 2011-05-17 12:01:53
@Gary Green:我想過,但他解決了完美的問題,所以我認爲這將是無關緊要的? – 2011-05-17 12:13:57