使用javascript获取特定的json值

时间:2015-10-05 16:30:02

标签: javascript json

我正在使用一些json数据,在结果集中,我有5组信息可供我选择。

在我的javascript中,当我调用行console.log(contacts.data["all"]);

时,我可以看到作为对象返回的数据

输出样本如下,来自chrome的调试窗口

0: Object allowanyoneedit: "True" allowedit: "1" charindex: "A" country: "AF" email: "xx@xx"

在我想要获取电子邮件信息的数据中

所以,如果我有;

0: Object
allowanyoneedit: "True"
allowedit: "1"
charindex: "A"
country: "AF"
email: "xx@xx"
1: Object
allowanyoneedit: "True"
allowedit: "1"
charindex: "A"
country: "AF"
email: "zz@xx"

我希望从json返回选项1:对象我将如何使用Javascript执行此操作 =========================编辑======================== =

这是我知道我正在通过数据获取数据的块

for (var x in companies) {
        var company = companies[x];
        var opt = $("<option>").attr({ value: company.id }).text(company.name);
        $("#contactcompany").append(opt);
        console.log(company);
        //$("#txtEmailTo").val();
        console.log(contacts.data["all"]);

在我正在使用的示例中,数组中有5个对象

========================编辑2 ===================== =========

Data += "{"
                For Each item In Columns.Split(",")
                    Dim Val As String = FormatJS(myReader(item).ToString)
                    If Val <> "" Then Data += """" & item.ToLower & """:""" & Val & ""","
                Next
                If CBool(myReader("AllowEdit").ToString) = True Then
                    Dim Val As String = FormatJS(myReader("AllowEdit").ToString)
                    If Val <> "" Then Data += """allowedit"":""" & Val & """"
                End If
                If Data.EndsWith(",") Then Data = Data.Remove(Data.Length - 1)
                Data += "},"

1 个答案:

答案 0 :(得分:0)

假设您的JSON如下: -

vat data = {
    0: {
        "allowanyoneedit": true, 
        "allowedit": 1, 
        "charindex": "A", 
        ...
    }, 
    1: {
    ...
    }
}

然后我会这样做:

console.log(data["1"]);

从1获取电子邮件,

console.log(data["1"]["email"])