javascript - 访问嵌入式 JSON 的深层对象成员

标签 javascript internet-explorer-11 ecmascript-5

以下函数在检索带有点标记的字符串的深层对象键值方面做得非常出色:

function getPathValue(obj, path) {
  return new Function('_', 'return _.' + path)(obj);
}

例如,它使用以下路径参数返回“bar”等键的值:

'data.abc.foo.bar'

但是,一些 API 将额外的字符串化 JSON 打包到一些键值中。

我正在寻找可以处理这种情况的上述功能的增强功能。

比如Stack自带的WebSocket服务:

wss://qa.sockets.stackexchange.com/

返回这样的字符串:

{"action":"155-questions-active","data":"{\"siteBaseHostAddress\":\"stackoverflow.com\",\"id\":53819390,\"titleEncodedFancy\":\"Should I start with webGL1 or webGL2 when using Three.js\",\"bodySummary\":\"Spent some time learning and understanding the basics of webGL and I'm now diving into Three.js.\\n\\nI noticed that I have the option of using webGL1 or webGL2. Seems as if webGL1 is the default, however ...\",\"tags\":[\"three.js\",\"webgl\"],\"lastActivityDate\":1545064508,\"url\":\"https://stackoverflow.com/questions/53819390/should-i-start-with-webgl1-or-webgl2-when-using-three-js\",\"ownerUrl\":\"https://stackoverflow.com/users/8226111/romanrogers\",\"ownerDisplayName\":\"romanrogers\",\"apiSiteParameter\":\"stackoverflow\"}"}

我希望能够使用上面的函数来检索带有输入字符串的值,例如:

'data.bodySummary'

也许输入的字符串看起来像这样:

'data/bodySummary'

其中斜线(/斜杠)表示 JSON.parse()。

请注意,这需要是动态的,因为我想让最终用户在一般情况下可以选择任意键来返回值。

最佳答案

您可以检查从中获取属性的项是否是字符串并执行解析。返回属性的值。

function getValue(object, path) {
    return path
        .split('.')
        .reduce((o, k) => (typeof o === 'string' ? JSON.parse(o) : o)[k], object);
}

var data = {"action":"155-questions-active","data":"{\"siteBaseHostAddress\":\"stackoverflow.com\",\"id\":53819390,\"titleEncodedFancy\":\"Should I start with webGL1 or webGL2 when using Three.js\",\"bodySummary\":\"Spent some time learning and understanding the basics of webGL and I'm now diving into Three.js.\\n\\nI noticed that I have the option of using webGL1 or webGL2. Seems as if webGL1 is the default, however ...\",\"tags\":[\"three.js\",\"webgl\"],\"lastActivityDate\":1545064508,\"url\":\"https://stackoverflow.com/questions/53819390/should-i-start-with-webgl1-or-webgl2-when-using-three-js\",\"ownerUrl\":\"https://stackoverflow.com/users/8226111/romanrogers\",\"ownerDisplayName\":\"romanrogers\",\"apiSiteParameter\":\"stackoverflow\"}"};

console.log(getValue(data, 'data.bodySummary'));

ES5

function getValue(object, path) {
    return path
        .split('.')
        .reduce(function (o, k) {
            return (typeof o === 'string' ? JSON.parse(o) : o)[k];
        }, object);
}

var data = {"action":"155-questions-active","data":"{\"siteBaseHostAddress\":\"stackoverflow.com\",\"id\":53819390,\"titleEncodedFancy\":\"Should I start with webGL1 or webGL2 when using Three.js\",\"bodySummary\":\"Spent some time learning and understanding the basics of webGL and I'm now diving into Three.js.\\n\\nI noticed that I have the option of using webGL1 or webGL2. Seems as if webGL1 is the default, however ...\",\"tags\":[\"three.js\",\"webgl\"],\"lastActivityDate\":1545064508,\"url\":\"https://stackoverflow.com/questions/53819390/should-i-start-with-webgl1-or-webgl2-when-using-three-js\",\"ownerUrl\":\"https://stackoverflow.com/users/8226111/romanrogers\",\"ownerDisplayName\":\"romanrogers\",\"apiSiteParameter\":\"stackoverflow\"}"};

console.log(getValue(data, 'data.bodySummary'));

关于javascript - 访问嵌入式 JSON 的深层对象成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53819904/

相关文章:

javascript - 通过文本而不是值设置 SELECT 元素

javascript - 智能表 : set page programmatically

javascript - 在 anchor 元素上 react 传递事件和参数

internet-explorer - 从 Canvas 下载图像在 IE 11 中不起作用

javascript - IE 11 中双击时的复选框状态

javascript - 文本未转换到正确的位置

java - IE11 : Error 400 only on specific server

javascript - ('eventName' , function(){...}); 上的这个模式 : . 的名称是什么?

javascript - 你什么时候使用 Object.defineProperty()

javascript - Eclipse 中有没有办法更改 ECMAScript 合规级别?