在jQuery中使用动态对象名称重新格式化JSON [英] Reformat JSON with dynamic object names in jQuery

查看:131
本文介绍了在jQuery中使用动态对象名称重新格式化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组简单的JSON,需要重新格式化,因为并非所有键值对都总是输出.

I have a simple set of JSON that needs to be reformatted since all of the key value pairs are not always output.

{
"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进行操作,而不能在服务器端进行操作.

The problem with the way this is output is depending on weather the data is available or not, accessing the objects by number could create unexpected functionality. The second problem is that is has to be manipulated by javascript, and cannot be manipulated on the server side.

我希望重新格式化JSON,以便每个类别都是一个对象(当前有3个,但可能多达5个)已在对象内部汇总了数据.例如:

I would like the JSON to be reformatted so that each category is an object (there are currently three, but could be as many as five) has summarized data inside the object. For example:

{
"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
        }
    ]

}
}

我将如何实现呢?仅仅循环浏览并命名对象无济于事.

How would I pull this off? Simply looping through and naming the objects is leading me nowhere.

推荐答案

作为T.J Crowder建议的补充:相同的基本原理,只是不依赖ES5功能,也不需要垫片. 注意:此解决方案与您的期望的输出" 确实有不同:我不是让每个类别都引用一个只有1个元素(对象文字)的数组,而是只需直接为其分配对象文字即可.您想要的格式将要求您访问退回的引荐,如下所示:obj.result.Referral[0].bounces,而我认为如果是obj.result.Referral.bounces则更有意义,而中间没有数组.

As an addition to T.J Crowder's suggestion: the same basic principle, only without relying on an ES5 function, nor requiring a shim. Note: This solution does differ from your "desired output" in one way: rather than having each category reference an array that has only 1 element (an object literal), I just assign it an object literal directly. Your desired format would require you to access the bounced referrals like so: obj.result.Referral[0].bounces, whereas I think it makes more sense if it were obj.result.Referral.bounces, without the array in between.

//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[i].category的值作为保存其他数据的对象的属性名称时,都会循环遍历由a.result引用的数组.
结果是:

This loops through the array, referenced by a.result, each time using the value of a.result[i].category as a property name for an object that holds the other data.
The result is:

console.log(JSON.stringify(b));
{"result":
    {"Negative Notification":
        {"event":"bounce",
         "result":16},
    "Referral":
        {"event":"open",
         "result":10},
    "Positive Notification":
        {"event":"bounce","result":176}
    }
}

但实际上:为什么在发送数据之前 不格式化数据,如果您有权访问输出该数据的代码,请更改该代码以更好地满足您的需求.

But really: why not format the data before you send it, if you have access to the code that outputs this data, change that code to better suite your needs.


为了回应您的评论,我想您的实际要求是:


In response to your comment, I think what you're actually after is this:

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如下所示:

After this code has run, object b looks like this:

{"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.bounce而不是使用b.result.Referral[0].bounce.但更重要的是,首先不需要该result属性:

That means that, instead of using b.result.Referral[0].bounce, you can use b.result.Referral.bounce. But even more importantly, there's no need for that result property in the first place:

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}
 }

这篇关于在jQuery中使用动态对象名称重新格式化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆