javascript - 数组作为对象对象输出

标签 javascript jquery arrays json

抱歉,如果我解释错误,但我有一个对象,它是一个数组,我试图获取要输出的字段,但我得到的只是 [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 的默认字符串表示形式(如果它不是 nullundefined) ,你可以在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, the toString() method is inherited by every object descended from Object. 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/

相关文章:

javascript - 货币字段的正则表达式

javascript - 如何遍历兄弟元素

arrays - 分割单词然后封装,然后分割字符封装

javascript - 使用 Vue.js 和 Minimalect 在 HTML 选择中显示默认值

javascript - jQuery:使用多个下拉菜单时获取 (1) 选择的值

javascript - 在原始架构的同一级别重用远程 JSON 架构中的属性

javascript - 创建动态不同的克隆表单

javascript - JQuery - 将字符串转换为对象并添加 "click"事件在模块模式中不起作用

c# - 算法:检查 3D 数组中的条件

javascript - 使用用于拆分的相同分隔符加入字符串