I have a json data, which need to convert in to separate key value pair to add in to a array.
what would be the correct way to make the json data into separate key pair data..?
I am looking for the best practice.
here is my json:
{
"DashBoard":[
{"title":"DashBoard"},
{"widget":[{"slide":"To do"},{"slide":"Teamspace"},{"slide":"Recent Activity"}]},
{"activity":[
{"option":[{"show":"Volvo"},{"show":"Benz"},{"show":"Honda"}]},
{"rows":[
{"row" : {"title":"Advert1", "text":"sample text1", "date":"22-06-2013"} }
,{"row" : {"title":"Advert2", "text":"sample text2", "date":"22-06-2014"} }
,{"row" : {"title":"Advert3", "text":"sample text3", "date":"22-06-2015"} }
,{"row" : {"title":"Advert4", "text":"sample text4", "date":"22-06-2016"} }
,{"row" : {"title":"Advert5", "text":"sample text5", "date":"22-06-2017"} }
]}
]}
]
}
my output should be:
var arr = [
{"title":"DashBoard"},
{"slide":"To do"},
{"slide":"Teamspace"},
{"slide":"Recent Activity"},
{"show":"Volvo"},
{"show":"Benz"},
{"show":"Honda"}
{"row" : {"title":"Advert1", "text":"sample text1", "date":"22-06-2013"},
{"row" : {"title":"Advert2", "text":"sample text2", "date":"22-06-2014"}
]
like so.
i try with :
newObj = {
"title" : obj[0],
"widget" : obj[1]["widget"],
"option" : obj[2].activity[0]["option"],
"rows" : obj[2].activity[1]["rows"]
};
newObj = {
"title" : obj[0],
"widget" : obj[1]["widget"],
"option" : obj[2].activity[0]["option"],
"rows" : obj[2].activity[1]["rows"]
};
$.each(newObj, function(key, value){
if($.type(value) === "object"){
newColl.push(value);
}else if ($.type(value) === "array"){
_.each(value, function(i,v){
newColl.push(i);
})
}
})
But not over come with best results. any one help me more correct way to get this? (basically i am converting this all in to models of backbone)