抱歉,如果我解释错误,但我有一个对象,它是一个数组,我试图获取要输出的字段,但我得到的只是 [object object]
$.getJSON( "https://service1.homepro.com/smart.asmx/GetFAP_ProfileReviewsJSON?bid=141772&sort=1&page=1", function( data ) {
// console.log(data);
xmlText = data;
var jsonObj = x2js.xml_str2json( xmlText );
// console.log(jsonObj.SMART);
var html = '<div class="review">';
$.each( jsonObj, function( key, answer ) {
// console.log('key', key);
console.log('answer', answer);
html += '<div>' + answer + '</div>';
// html += '<div>' + key + '</div>';
});
$('div').html(html);
});
任何人都可以帮助我或告诉我哪里出了问题吗?
谢谢
最佳答案
在 JavaScript 中 [object object]
是 JavaScript object
的默认字符串表示形式(如果它不是 null
或 undefined)
,你可以在JavaScript toString() Reference中看到那:
Every object has a
toString()
method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, thetoString()
method is inherited by every object descended fromObject
. If this method is not overridden in a custom object,toString()
returns "[object type]
", where type is the object type. The following code illustrates this:
var o = new Object();
o.toString(); // returns [object Object]
Note: Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [object Undefined], as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using_toString()_to_detect_object_class.
解决方案:
- 如果您想打印您的
对象
,您需要编写一个自定义 函数将通过生成自定义表示来为您完成此操作 根据对象的属性来确定对象的属性。 - 您还可以使用
JSON.stringify(answer)
它将返回您的对象 作为字符串,但我认为它不会给你想要的输出 参见。
关于javascript - 数组作为对象对象输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683805/