如何解码 JSON 字符串?

发布于 2024-11-07 13:55:05 字数 2028 浏览 0 评论 0原文

在服务器端,我有 2 个哈希值编码成 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

On the server side do I have 2 hashes I encode into JSON strings like so

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;

On the client side I have

$(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 contains

{"ss":"Sandra Schlichting","fn":"Full name"}

but it is not an array. When I use $.each() it takes on character at a time.

Problem

How do I turn it into an array, so I can use

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);
}

which should produce

Initials    Full Name
ss  Sandra Schlichting
fn  Full name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏雪无痕 2024-11-14 13:55:05

您应该使用 http://api.jquery.com/jQuery.getJSON 中提到的 jQuery.getJSON() /

如果你想这样做,还有 $.parseJSON() 方法将字符串解析为 json。

You should use jQuery.getJSON() as mentioned at http://api.jquery.com/jQuery.getJSON/.

There is also $.parseJSON() method to parse string to json if you want to go that way.

落日海湾 2024-11-14 13:55:05

您不需要将其转换为数组。根据 jQuery.each() 文档 它接受数组或对象,并且 JSON 是一个子集JavaScript 的对象字面量表示法。

编辑:这是一个示例: http://jsfiddle.net/pedrocorreia/s5UrZ/2/

You don't need to turn it into an array. According to the jQuery.each() documentation it takes both arrays or objects and JSON is a subset of the object literal notation of JavaScript.

Edit: Here's an example: http://jsfiddle.net/pedrocorreia/s5UrZ/2/

贱人配狗天长地久 2024-11-14 13:55:05

您可以使用 douglas crockford 创建的 JSON 解析器:
https://github.com/douglascrockford/JSON-js

包含 json2.js 在您的页面中,您可以执行以下操作:

var object = JSON.parse(string);

然后您可以将其用作数组。

You can use the JSON parser created by douglas crockford:
https://github.com/douglascrockford/JSON-js

include the json2.js in your page, the you can do:

var object = JSON.parse(string);

Then you can use it as an array.

毅然前行 2024-11-14 13:55:05

您可以在声明中使用 for

var index = 0;
for(user in users){
  result += '<tr><td>' + index + '</td><td>' + user['fn'] + '</td></tr>\n';
index++;
}

you can use for in statement

var index = 0;
for(user in users){
  result += '<tr><td>' + index + '</td><td>' + user['fn'] + '</td></tr>\n';
index++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文