如何在javascript中从JSON对象访问数组?

时间:2013-11-21 13:01:03

标签: javascript json

您好我想问一下如何从JSON对象访问javascript中的数组,如下所示:

Object {results: "success", message: "Your message have been sent successfully",           error_id_array: Array[1]}
  error_id_array: Array[1]
    0: "2"
    length: 1
    __proto__: Array[0]
  message: "Your message have been sent successfully"
  results: "success"
  __proto__: Object

此问题与此How to work with array returned from PHP script using AJAX?问题有关。谢谢

1 个答案:

答案 0 :(得分:1)

正如Moo-Juice所说,这不是一个有效的对象。以下是有效对象的示例以及如何访问其属性:

var myObj = {
    myString : 'Hello world!',
    myArray : ['red', 'yellow', 'blue'],
    alertString : function() {
        var message = this.myString;
        for (var i = 0; i < this.myArray.length; i++) {
            message += ' ' + this.myArray[i];
        }
        alert(message);
    }

};

myObj.alertString();

// triggers a modal that says 'Hello world! red yellow blue'