【问题标题】:How to access an object's contents in javascript?如何在javascript中访问对象的内容?
【发布时间】:2011-06-23 11:34:32
【问题描述】:

当我这样做时

$.each(result, function(i, n){
alert("key: " + i + ", Value: " + n );
});

然后对于我看到的每次迭代

key: 276, Value: {"owners":["he"],"users":["he","m"],"end":"07/06-2011","groups":[],"type":"in"}

如何在每次迭代中访问 ownersusersendgroupstype 的值?

在 Perl 中我会这样做

foreach my $key (keys %result) {
   print $result{$key}{owners};
   print $result{$key}{users};
   ...
}

更新

我从 JSON 中得到 result,就像这样

$.ajax({
    type: "GET",
    url: "/cgi-bin/ajax.pl",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "cwis" : id },
    // ...
    success: function(result){
    if (result.error) {
        alert('result.error: ' + result.error);
    } else {

        $.each(result, function(i, n){
        alert( "key: " + i + ", Value: " + n );

        });


    }
    }
});

更新 2

问题似乎是服务器端没有发送探测器 JSON。

这是生成 JSON 字符串的服务器端脚本。

!/usr/bin/perl -T

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use JSON;
use utf8;
use strict;
use warnings;

my $cgi = CGI->new;
$cgi->charset('UTF-8');

my $json_string = qq{{"error" : "The user do not have any activities."}};

my $json = JSON->new->allow_nonref;
$json = $json->utf8;

# @a and $act is now available

my $data;
foreach my $id (@a) {
    $data->{$id} = $json->encode(\%{$act->{$id}});
}
$json_string = to_json($data);


print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;

【问题讨论】:

  • 能不能也显示一下perl脚本生成的结果?
  • 我不是 CGI 专家,但看起来您正在将数据双重编码为​​ JSON。一次使用 my $json = JSON->new->allow_nonref; $json = $json->utf8; ,然后再次使用 $data->{$id} = $json->encode(\%{$act->{$id}}) 。不能说我以前曾尝试过这样做!
  • @T9b :你完全正确!那就是问题=)如果您发布是作为解决方案,那么我接受它=)非常感谢=)
  • @ Sandra Schlichting - 我将在下面更新我的答案。

标签: javascript jquery perl


【解决方案1】:
document.write(result[key].owners);
document.write(result[key].users);

更新:

显然我对这个问题的评论就是答案:

我不是 CGI 专家,但它看起来像 您将数据双重编码为 JSON。曾经与

my $json = JSON->new->allow_nonref; $json = $json->utf8; 

然后再用

$data->{$id} = $json->encode(\%{$act->{$id}}) 。

【讨论】:

  • 您是否有机会尝试在值中创建 JSON?您确定它的格式正确,然后解析正确吗?您是手动生成的还是从某个提要中接收到的?
  • result 来自 JSON,所以我试图访问已被 JSON 解码的数据。 1 分钟后我会用 Ajax 调用更新问题。
  • 你能把它改成alert( "key: " + i + ", Type: " + (typeof n) );并告诉我们结果吗?看起来 JSON 似乎没有以某种方式正确解析。
  • @Björn :它说它是字符串。几分钟后,我将发布服务端脚本。
【解决方案2】:

$.each回调中,this指向当前元素,所以

$.each(result, function(i, n){
     alert(this.users);
});

【讨论】:

  • 当我尝试这样做时,我收到了undefined
  • @stereofrog :就我而言,数据看起来不同。请注意,我的 Value 中的所有内容都只包含在一组 {} 中。数据来自 JSON,如果这有什么不同?
  • @Sandra 我猜你在第一次迭代时得到了undefined ninteger
  • @stereofrog :根据 Teneff 的回答,我的 result 不是一个对象。我想这就是问题所在?
【解决方案3】:
n.owners or n['owners']
n.users or n['users']
etc.

在循环中...

$.each(result, function(k,v) {
    console.log("key: " + k + ", value: " + v );
    $.each(v, function(k,v)) {
        console.log("key: " + k + ", value: " + v );
    });
});

【讨论】:

  • 我在这两种情况下也得到undefined
【解决方案4】:

您可以像这样访问它们:

n.owners

n['owners']

或者你可以使用另一个循环:

$.each(result, function(i, n){
    if (typeof(n)=='object') {
        $.each(n, function(k, v){
            alert('n.'+k+' = ' + v);
        });
    }
});

编辑: jsFiddle Example Example 2

edit2:为了避免得到undefined,做一个简单的检查i的键是否等于"Value",所以它的值将是一个对象

【讨论】:

  • 好的,这很有趣。当我在我的数据上使用来自 jsFiddle 的代码时,我收到了 is not an object
  • @Sandra just "is ..." ... 没有键名?
  • 当我在我的数据上使用示例 2 中的代码时,它总是输入您的 else-condition。
  • @Sandra 尝试 alert(typeof(n)); 并检查 n 究竟是什么
  • @Teneff :它返回字符串。所以我猜问题出在服务器端?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多