最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

访问数组和对象内JSON

IT培训 admin 5浏览 0评论

访问数组和对象内JSON

我将此称为get API返回的XML和我是转换成JSON,但xml2js返回[对象] [通知]和元素数组中[阵列。我怎么能看到什么元素数组里面?

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var convert = require('xml-js');
var request = new XMLHttpRequest();
request.open("GET", url, true, username, password);

request.withCredentials = true;

request.send();
request.onreadystatechange=(e)=>{

    var obj = convert.xml2js(request.responseText);

console.log(obj);

下面是输出:

{ declaration:
    { attributes: { version: '1.0', encoding: 'UTF-8', standalone: 'yes' } },
   elements:
     [ { type: 'element',
         name: 'model-response-list',
         attributes: [Object],
         elements: [Array] } ] }
回答如下:

默认节点输出控制台隐藏深深嵌套的对象/阵列。 能够避免此问题:

  • 与指定console.dir选项depth
  • 对象转换为JSON字符串
> var obj = { a: { b: { c: { d: {} } } } };

> console.log(obj);
{ a: { b: { c: [Object] } } }

> console.dir(obj, { depth: null }); // null for unlimited recursion
{ a: { b: { c: { d: {} } } } }

> console.log(JSON.stringify, null, 4); // JSON.stringify can also format input with white spaces (in this case - 4)
{
    "a": {
        "b": {
            "c": {
                "d": {}
            }
        }
    }
}

访问数组和对象内JSON

我将此称为get API返回的XML和我是转换成JSON,但xml2js返回[对象] [通知]和元素数组中[阵列。我怎么能看到什么元素数组里面?

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var convert = require('xml-js');
var request = new XMLHttpRequest();
request.open("GET", url, true, username, password);

request.withCredentials = true;

request.send();
request.onreadystatechange=(e)=>{

    var obj = convert.xml2js(request.responseText);

console.log(obj);

下面是输出:

{ declaration:
    { attributes: { version: '1.0', encoding: 'UTF-8', standalone: 'yes' } },
   elements:
     [ { type: 'element',
         name: 'model-response-list',
         attributes: [Object],
         elements: [Array] } ] }
回答如下:

默认节点输出控制台隐藏深深嵌套的对象/阵列。 能够避免此问题:

  • 与指定console.dir选项depth
  • 对象转换为JSON字符串
> var obj = { a: { b: { c: { d: {} } } } };

> console.log(obj);
{ a: { b: { c: [Object] } } }

> console.dir(obj, { depth: null }); // null for unlimited recursion
{ a: { b: { c: { d: {} } } } }

> console.log(JSON.stringify, null, 4); // JSON.stringify can also format input with white spaces (in this case - 4)
{
    "a": {
        "b": {
            "c": {
                "d": {}
            }
        }
    }
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论