使用Jquery GET

时间:2015-09-20 10:08:57

标签: javascript jquery arrays json ajax

我使用Jquery GET获取一些如下所示的JSON数据:

{
    "list": {
        "meta": {
            "type": "resource-list",
            "start": 0,
            "count": 1
        },
        "resources": [
            {
                "resource": {
                    "classname": "Quote",
                    "fields": {
                        "change": "-0.400002",
                        "chg_percent": "-1.200485",
                        "day_high": "33.779999",
                        "day_low": "32.549999",
                        "issuer_name": "PayPal Holdings, Inc.",
                        "issuer_name_lang": "PayPal Holdings, Inc.",
                        "name": "PYPL",
                        "price": "32.919998",
                        "symbol": "PYPL",
                        "ts": "1442606400",
                        "type": "equity",
                        "utctime": "2015-09-18T20:00:00+0000",
                        "volume": "16488139",
                        "year_high": "42.550000",
                        "year_low": "30.000000"
                    }
                }
            }
        ]
    }
}

我想获得day_high的价值。使用Jquery我做:

jQuery.ajax({
    url: "http://****.com",
    type: "GET",
    dataType: "json",
    async: false,
    success: function (data) {
        var x = JSON.stringify(data.list.resource.change);
       $("p.name").append(x);
       console.log(x.change);
    }
});

在我的控制台中,我得到:

Uncaught TypeError: Cannot read property 'change' of undefined

我也尝试了像data.list.resource[0].change这样的数组方法但输出:

Uncaught TypeError: Cannot read property '0' of undefined 

2 个答案:

答案 0 :(得分:2)

首先,变量x不能用您的控制台日志记录语句解释为JSON,因为JSON.stringify将其转换为字符串。其次,看起来你的道路混乱了。它应该是data.list.resources[0].resource.fields.change

答案 1 :(得分:1)

你可以试试这个:

 Dim con As New MySqlConnection("host=localhost; username=root; password=; database=pawnshop")
Dim dr As MySqlDataReader
Dim cmd As New MySqlCommand

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click


    cmd.CommandText = " select clientid from clients where clientid= '" & txtId.Text & "'"

    dr = cmd.ExecuteReader

    If dr.HasRows Then
        dr.Close()
        cmd.CommandText = "SELECT * FROM clients where clientid = '" & txtId.Text & "'"
        cmd.Connection = con
        dr = cmd.ExecuteReader

        If dr.HasRows Then

            dr.Read()
            txtFname.Text = dr.Item("fname")
            txtMname.Text = dr.Item("mname")

        End If
    Else
        MsgBox("Customer ID is not Found")
    End If
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    dr.Close()
    cmd.CommandText = " select clientid from clients where clientid= '" & txtId.Text & "'"
    dr = cmd.ExecuteReader
    If dr.HasRows Then
        dr.Close()
        cmd.CommandText = "insert into pawn (clientid, category, type, gram, karat, date_added, description) values('" & txtId.Text & "','" & txtCategory.Text & "','" & txtType.Text & "','" & txtGram.Text & "','" & txtKarat.Text & "','" & DateTimePicker_DOB.Text & "','" & txtDesc.Text & "')"
        dr = cmd.ExecuteReader
        MsgBox("Successfully Added")

    Else
        MsgBox("Customer ID not Found")

    End If
End Sub

Private Sub frmExisting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.Open()
    cmd.Connection = con
End Sub

错误与您的路径有关。