[英]To print all the paths in a json object
What is the easy way to get all paths in the Given Json Object;获取给定 Json 对象中所有路径的简单方法是什么? For Example:例如:
{
app:{
profiles:'default'
},
application:{
name:'Master Service',
id:'server-master'
},
server:{
protocol:'http',
host:'localhost',
port:8098,
context:null
}
}
I should able to produce the following object我应该能够产生以下对象
app.profiles=default
application.name=Master Service
application.id=server-master
I was able to achieve the same using a recursive function.我能够使用递归函数实现相同的目标。 I want to know is there any built in function from json which does this.我想知道 json 中是否有任何内置函数可以执行此操作。
You can implement your custom converter by iterating through objects recursively.您可以通过递归遍历对象来实现您的自定义转换器。
Something like this:像这样的东西:
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');
That's not a best converter implementation, just a fast-written example, but it should help you to understand the main principle.这不是最好的转换器实现,只是一个快速编写的示例,但它应该可以帮助您理解主要原理。
Here is the working JSFiddle demo .这是工作中的 JSFiddle 演示。
I think there is no built-in function that does this.我认为没有内置函数可以做到这一点。 It can be done with a simple for loop as below in my fiddle.它可以通过一个简单的 for 循环来完成,如下面我的小提琴。 But it does not take care of recursiveness.但它不考虑递归。 Here are other posts that i found regarding the same : Post1 and Post2 and Post3以下是我发现的其他帖子: Post1和Post2和Post3
var myjson = {
app:{
profiles:'default'
},
application:{
name:'Master Service',
id:'server-master'
},
server:{
protocol:'http',
host:'localhost',
port:8098,
context:null
}
};
for(key in myjson) {
for(k in myjson[key]) {
console.log(key + '.' + k + ' = '+ myjson[key][k]);
}
}
var obj1 = {
"name" : "jane",
"job" : {
"first" : "nurse",
"second" : "bartender",
"last" : {
"location" : "Paris",
"work" : ["sleep", "eat", "fish", {"math" : "morning", "chem" : "late night"}],
"read_books": {
"duration" : 10,
"books_read" : [{"title" : "Gone with the wind", "author": "I don't know"}, {"title": "Twologht", "author": "Some guy"}]
}
}
},
"pets": ["cow", "horse", "mr. peanutbutter"],
"bf" : ["jake", "beatles", {"johnson": ["big johnson", "lil johnson"]}]
}
var obj2 = ["jane", "jake", {"age" : 900, "location": "north pole", "name" : "Jim", "pets" : "..."}];
var allRoutes = [];
function findTree(o, parentKey = '', parentObject)
{
if (typeof o !== 'object')
{
if (parentKey.substr(0,1) === '.')
allRoutes.push(parentKey.substr(1));
else
allRoutes.push(parentKey);
return;
}
let keys = Object.keys(o);
for (let k in keys)
{
findTree(o[keys[k]], Array.isArray(o) ? parentKey + "[" +keys[k] + "]" : parentKey + "." +keys[k], o);
//findTree(o[keys[k]], parentKey + "[" +keys[k] + "]");
}
}
findTree(obj1);
Try this.尝试这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.