使用javascript获取特定的json值

get specific json values using javascript

本文关键字:json javascript 获取 使用      更新时间:2023-09-26

我正在处理一些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 += "},"

假设您的JSON如下:-

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

那么我会做:

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

从1,

获取电子邮件
console.log(data["1"]["email"])