Javascript 重新构造JSON
好了,伙计们需要帮忙。我还在学习Javascript及其与JSON的交互。我有一个类似这样的JSONJavascript 重新构造JSON,javascript,json,Javascript,Json,好了,伙计们需要帮忙。我还在学习Javascript及其与JSON的交互。我有一个类似这样的JSON [{ "categories":"Food", "subcategories":"Shares", "pid":"111", "title":"Smoked Salmon Dip", "description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustade
[{
"categories":"Food",
"subcategories":"Shares",
"pid":"111",
"title":"Smoked Salmon Dip",
"description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustades"
},
{
"categories":"Food",
"subcategories":"Shares",
"pid":"112",
"title":"Sweet Goat Cheese Flatbread",
"description":"Delicate grilled Naan flatbread coated with delish tomato jam and topped with melted goat cheese, roasted garlic, fennel, onion, pear, shiitake mushroom and arugula."
},
{
"categories":"Food",
"subcategories":"Snacks",
"pid":"100",
"title":"Beer Chili",
"description":"Hot & satisfying short rib chili with black beans, smoked jalapenos, and fresh corn. Topped with aged cheddar cheese and sour cream."
}];
{
"menu":{
"categories":[
{
"name":"Food",
"subcategories":[
{
"name":"Shares",
"items":[
{
"pid":"111",
"title":"Smoked Salmon Dip",
"description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustades"
},
{
"pid":"112",
"title":"Sweet Goat Cheese Flatbread",
"description":"Delicate grilled Naan flatbread coated with delish tomato jam and topped with melted goat cheese, roasted garlic, fennel, onion, pear, shiitake mushroom and arugula."
}
]
},
{
"name":"Snacks",
"items":[
{
"pid":"100",
"title":"Beer Chili",
"description":"Hot & satisfying short rib chili with black beans, smoked jalapenos, and fresh corn. Topped with aged cheddar cheese and sour cream."
}
]
}
]
}]
}
}
但我需要的是一个JSON,看起来像这样
[{
"categories":"Food",
"subcategories":"Shares",
"pid":"111",
"title":"Smoked Salmon Dip",
"description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustades"
},
{
"categories":"Food",
"subcategories":"Shares",
"pid":"112",
"title":"Sweet Goat Cheese Flatbread",
"description":"Delicate grilled Naan flatbread coated with delish tomato jam and topped with melted goat cheese, roasted garlic, fennel, onion, pear, shiitake mushroom and arugula."
},
{
"categories":"Food",
"subcategories":"Snacks",
"pid":"100",
"title":"Beer Chili",
"description":"Hot & satisfying short rib chili with black beans, smoked jalapenos, and fresh corn. Topped with aged cheddar cheese and sour cream."
}];
{
"menu":{
"categories":[
{
"name":"Food",
"subcategories":[
{
"name":"Shares",
"items":[
{
"pid":"111",
"title":"Smoked Salmon Dip",
"description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustades"
},
{
"pid":"112",
"title":"Sweet Goat Cheese Flatbread",
"description":"Delicate grilled Naan flatbread coated with delish tomato jam and topped with melted goat cheese, roasted garlic, fennel, onion, pear, shiitake mushroom and arugula."
}
]
},
{
"name":"Snacks",
"items":[
{
"pid":"100",
"title":"Beer Chili",
"description":"Hot & satisfying short rib chili with black beans, smoked jalapenos, and fresh corn. Topped with aged cheddar cheese and sour cream."
}
]
}
]
}]
}
}
我知道这有点难看,但在迭代当前的JSON时,我很难弄清楚如何构建新的JSON
任何能让我继续的帮助都是非常棒的我制定了一个解决方案,但我不确定它将如何处理大的json数据。假设json变量存储您的数据:
categories = [];
json.forEach(function(entry) {
var cindex = categories.map(function(category) {
return category.name;
}).indexOf(entry.categories);
if (cindex < 0) {
// Not found in categories array
cindex = categories.push({
name: entry.categories,
subcategories: []
}) - 1; // -1 to fix the index
}
// Lets search the subcategory
var category = categories[cindex];
var sindex = category.subcategories.map(
function(subcategory) {
return subcategory.name;
}
).indexOf(entry.subcategories);
if (sindex < 0) {
// Not Found
sindex = category.subcategories.push({
name: entry.subcategories,
items: []
}) - 1;
}
// Subcategory exists. Just push
category.subcategories[sindex].items.push({
pid: entry.pid,
description: entry.description,
title: entry.title
});
});
var menu = {
menu: {
categories: categories
}
};
我试一试,不妨公布我的结果。数据与您拥有的数据并不完全一致。它不需要jQuery,并且可以用于多次对数据进行排序
Object.prototype.groupBy = function(itemName, preGrouped)
{
if(preGrouped)
{
for(prop in this)
{
if(typeof(this[prop]) === 'object' && this[prop].groupBy !== undefined)
{
var reGroup = this[prop].groupBy(itemName);
this[prop] = reGroup;
}
}
return this;
}
else
{
var uniqueItems = {};
var uniqueItemLength = 0;
for(var i=0, length=this.length; i < length; i++)
{
var item = this[i];
var z =0;
var found = false;
while(z < uniqueItemLength)
{
if(item[itemName] in uniqueItems)
{
uniqueItems[item[itemName]].push(item);
found = true;
break;
}
z++;
}
if(!found)
{
uniqueItems[item[itemName]] = [];
uniqueItems[item[itemName]].push(item);
uniqueItemLength++;
}
}
return uniqueItems;
}
}
贝特拉巴的回答是正确的。我花了一些时间在这一个,并认为我至少张贴。也许它会帮助其他人
你为什么选择那个结构?与其使用具有名称属性的对象数组,为什么不使用属性等于建议名称值的对象来进行爆炸呢。这将馈送到现有的胡子模板中。@Beterraba我开始做类似的事情,但随后绘制了一个空白forvar I=0;我为什么不试着把它们构建成完整的JS对象,然后对它们进行字符串化。谢谢@Beterraba,这正是我需要的。我将用它作为我学习的例子。谢谢,againI注意到,除非jQuery可用,否则这是行不通的。您能指出jquery特定的项吗?@ddp这里是json.forEach。它不是jQuery,但一些浏览器IE8之后仍然没有实现。您可以查看更多详细信息。只需将json.forEach更改为。。;。。;。。你会很好的,我想就是这样。谢谢感谢@cgatian,正如我之前所说的,我从这个例子中学到了最好的东西,看到两种不同的方法可以帮助我学到更多。