如何在JSON结果中展平关联对?

时间:2012-02-02 20:48:42

标签: javascript jquery json extjs

我从包含对象和关联表的服务器接收数组。例如,我从服务器获得了这个JSON结果:

var myEvent = {
   "Event":{
      "SessionTags":[
         {
            "SessionID":1,
            "TagID":x
         },
         {
            "SessionID":2,
            "TagID":x
         },
         {
            "SessionID":2,
            "TagID":y
         },
         {
            "SessionID":3,
            "TagID":z
         }
      ],
      "Sessions":[
         {
            "ID":1,
            "Name":"Advanced Tips",
         },
         {
            "ID":2,
            "Name":"Best Practices"
         },
         {
            "ID":3,
            "Name":"Code Fun"
         },
      "Tags":[
         {
            "ID":x,
            "Name":"AJAX"
         },
         {
            "ID":y,
            "Name":"Android"
         },
         {
            "ID":z,
            "Name":"ASP.NET"
         },
      ]
   }
}

注意用作关联表的“SessionTags”对象。如何在会话标记中添加正确的标记对象,以便我最终得到:

var myNewEvent = {
   "Event":{
      "Sessions":[
         {
            "ID":1,
            "Name":"Advanced Tips",
            "Tags":[
              {
               "ID":x,
               "Name":"AJAX"
              }
            ]
         },
         {
            "ID":2,
            "Name":"Best Practices",
            "Tags":[
              {
               "ID":x,
               "Name":"AJAX"
              },
              {
               "ID":y,
               "Name":"Android"
              }
            ]
         },
         {
            "ID":3,
            "Name":"Code Fun",
            "Tags":[
              {
               "ID":z,
               "Name":"ASP.NET"
              }
            ]
         }
   }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

var getTagName = function(tagId){
    var t = myEvent["Event"]["Tags"];
    for (var i in t); {
        if (t[i].ID == tagId) {
            return t[i].Name;
        }
    }
};

var getTags = function(sessionId){
    var s = myEvent["Event"]["SessionTags"];
    var tags = [];
    for (var i in s) {
        if (s[i]["SessionID"] == sessionId) {
            var tagName = getTagName(s[i]["TagID"]);
            tags.push({"ID": sessionId, "Name": tagName});
        }
    }
    return tags;
};

var addSessionTags = function(){
    var s = myEvent["Event"]["Sessions"];
    for (var i in s) {
        var currentSession = s[i];
        currentSession.Tags = getTags(currentSession.ID);
    }
};

addSessionTags();

delete myEvent.Event.Tags;
delete myEvent.Event.SessionTags;

我必须清理你的数据稍微(x,y,z的ID没有引用任何变量,你还有一些额外的逗号和一个缺少的右括号)来获取它对于这个例子是可行的:

var myEvent = {
   "Event":{
      "SessionTags":[
         {
            "SessionID":1,
            "TagID":"x"
         },
         {
            "SessionID":2,
            "TagID":"x"
         },
         {
            "SessionID":2,
            "TagID":"y"
         },
         {
            "SessionID":3,
            "TagID":"z"
         }
      ],
      "Sessions":[
         {
            "ID":1,
            "Name":"Advanced Tips",
         },
         {
            "ID":2,
            "Name":"Best Practices"
         },
         {
            "ID":3,
            "Name":"Code Fun"
         }
     ],
      "Tags":[
         {
            "ID":"x",
            "Name":"AJAX"
         },
         {
            "ID":"y",
            "Name":"Android"
         },
         {
            "ID":"z",
            "Name":"ASP.NET"
         }
      ]
   }
}