Ysa*_*sak 6 javascript json jsonpath node.js gulp
获取Given Json对象中所有路径的简单方法是什么?例如:
{
app:{
profiles:'default'
},
application:{
name:'Master Service',
id:'server-master'
},
server:{
protocol:'http',
host:'localhost',
port:8098,
context:null
}
}
Run Code Online (Sandbox Code Playgroud)
我应该能够生成以下对象
app.profiles=default
application.name=Master Service
application.id=server-master
Run Code Online (Sandbox Code Playgroud)
我能够使用递归函数实现相同的功能.我想知道json是否有内置函数可以做到这一点.
您可以通过递归迭代对象来实现自定义转换器.
像这样的东西:
var YsakON = { // YsakObjectNotation
stringify: function(o, prefix) {
prefix = prefix || 'root';
switch (typeof o)
{
case 'object':
if (Array.isArray(o))
return prefix + '=' + JSON.stringify(o) + '\n';
var output = "";
for (var k in o)
{
if (o.hasOwnProperty(k))
output += this.stringify(o[k], prefix + '.' + k);
}
return output;
case 'function':
return "";
default:
return prefix + '=' + o + '\n';
}
}
};
var o = {
a: 1,
b: true,
c: {
d: [1, 2, 3]
},
calc: 1+2+3,
f: function(x) { // ignored
}
};
document.body.innerText = YsakON.stringify(o, 'o');Run Code Online (Sandbox Code Playgroud)
这不是最好的转换器实现,只是一个快速编写的示例,但它应该有助于您理解主要原理.
| 归档时间: |
|
| 查看次数: |
2693 次 |
| 最近记录: |