重构JSON [英] Re-Structuring a JSON
本文介绍了重构JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的家伙需要一些帮助。我还在学习Javascript及其交互JSON。我有这样的JSON
OK guys need some help out there. I'm still learning Javascript and its interactions JSON. I have a a JSON like this
[{
"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."
}];
但我需要的是一个看起来像这样的JSON
But what I need is an JSON that looks like this
{
"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。
I know its a bit ugly and but I'm having trouble figuring out how to build the new JSON as i iterate through my current one.
让我前进的任何帮助都很棒
Any help getting me going would be awesome
推荐答案
我锻炼了一个解决方案,但我'我不知道它将如何处理大的json数据。假设 json
var存储您的数据:
I workout a solution, but I'm not sure how it will handle big json data. Assume that the json
var stores your data:
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
}
};
这篇关于重构JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文