我有一组简单的 JSON,需要重新格式化,因为并非总是输出所有键值对。
{
"result": [
{
"category": "Negative Notification",
"event": "open",
"result": 2
},
{
"category": "Referral",
"event": "bounce",
"result": 1
},
{
"category": "Negative Notification",
"event": "delivered",
"result": 34
},
{
"category": "Negative Notification",
"event": "processed",
"result": 34
},
{
"category": "Positive Notification",
"event": "open",
"result": 42
},
{
"category": "Referral",
"event": "delivered",
"result": 17
},
{
"category": "Positive Notification",
"event": "processed",
"result": 504
},
{
"category": "Referral",
"event": "processed",
"result": 18
},
{
"category": "Positive Notification",
"event": "delivered",
"result": 504
},
{
"category": "Negative Notification",
"event": "bounce",
"result": 16
},
{
"category": "Positive Notification",
"event": "bounce",
"result": 176
},
{
"category": "Referral",
"event": "open",
"result": 10
}
]
}
输出方式的问题取决于数据是否可用的天气,按数字访问对象可能会产生意外的功能。第二个问题是它必须由javascript操作,并且不能在服务器端操作。
我希望重新格式化 JSON,以便每个类别都是一个对象(当前有 3 个,但可能多达 5 个),并在对象内汇总了数据。例如:
{
"result": {
"Negative Notification" : [
{
"processed":34,
"delivered":34,
"bounces":16,
"opens":2
}
],
"Positive Notification" : [
{
"processed":504,
"delivered":504,
"bounces":176,
"opens":42
}
],
"Referral" : [
{
"processed":18,
"delivered":17,
"bounces":1,
"opens":10
}
]
}
}
我该如何完成这个任务?简单地循环并命名对象不会给我带来任何结果。
最佳答案
作为 T.J Crowder 建议的补充:基本原理相同,只是不依赖 ES5 函数,也不需要垫片。 注意:此解决方案在某种程度上与您的“所需输出”有所不同:我不是让每个类别引用只有 1 个元素(对象文字)的数组,而是只需直接为其分配一个对象文字即可。您想要的格式将要求您访问退回的推荐,如下所示:obj.result.Referral[0].bounces
,而我认为如果是 obj.result.Referral.bounces
更有意义,中间没有数组。
//suppose a is the raw JSON data
var b = {result:{}};//this will become be the object you want
for (var i=0;i<a.result.length;i++)
{
b.result[a.result[i].category] = (function(obj)
{
var p, res = {};
for (p in obj)
{
if (p !== 'category' && obj.hasOwnProperty(p))
{
res[p] = obj[p];
}
}
return res;
}(a.result[i]));
}
这会循环遍历由 a.result
引用的数组,每次使用 a.result[i].category
的值作为保存其他数据的对象的属性名称。
结果是:
console.log(JSON.stringify(b));
{"result":
{"Negative Notification":
{"event":"bounce",
"result":16},
"Referral":
{"event":"open",
"result":10},
"Positive Notification":
{"event":"bounce","result":176}
}
}
但实际上:为什么不在发送数据之前格式化数据呢?如果您有权访问输出此数据的代码,请更改该代码以更好地满足您的需求。
编辑:
针对您的评论,我认为您真正想要的是:
var b={result{}};
for (i=0;i<a.result.length;i++)
{
b.result[a.result[i].category] = b.result[a.result[i].category] || {};//use existing, or create new object
b.result[a.result[i].category][a.result[i].event] = a.result[i].result;//add property for event type, assign value
}
运行此代码后,对象 b
看起来像这样:
{"result":
{"Negative Notification":
{"open":2,
"delivered":34,
"processed":34,
"bounce":16},
"Referral":
{"bounce":1,
"delivered":17,
"processed":18,
"open":10},
"Positive Notification":
{"open":42,
"processed":504,
"delivered":504,
"bounce":176}
}
}
这意味着,不要使用 b.result.Referral[0].bounce
,您可以使用b.result.Referral.bounce
。但更重要的是,没有必要 result
首先的属性:
var result ={};
for (i=0;i<a.result.length;i++)
{
result[a.result[i].category] = result[a.result[i].category] || {};
result[a.result[i].category][a.result[i].event] = a.result[i].result;
}
console.log(result);
{"Negative Notification":
{"open":2,
"delivered":34,
"processed":34,
"bounce":16},
"Referral":
{"bounce":1,
"delivered":17,
"processed":18,
"open":10},
"Positive Notification":
{"open":42,
"processed":504,
"delivered":504,
"bounce":176}
}
关于javascript - 在 jQuery 中使用动态对象名称重新格式化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16407879/