我有数组值。需要将此数组值转换为json格式。示例给出了bleow
样本数组
[Management Portal!@!@Production Issue Handling!@!@/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!@!@ Event Browser!@!@/IONSWeb/orderManagement/eventBrowser.do, Management Portal!@!@ Order Workflow!@!@/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!@!@Admin Message!@!@/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!@!@Audit!@!@/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!@!@Locks!@!@/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!@!@Queue!@!@/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!@!@Role!@!@/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!@!@Routing Rule!@!@/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!@!@Task Code!@!@/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!@!@Trigger OutEvent!@!@/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!@!@User!@!@/IONSWeb/userManagement/begin.do, ADMINISTRATION!@!@Refresh Application Cache!@!@/IONSWeb/userManagement/refreshApplnCache.do]
示例Json
{
"name": "Administration",
"sub": [
{
"name": "Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "Infrastructure sonet Add Order ",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "fGNS Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
}
]
}
请任何人帮忙
答案 0 :(得分:1)
我想你想做这样的事情。拆分字符串,取出第一个元素,即名称,并遍历所有元素。每个偶数值都是名称,每个奇数都是网址。
如果是奇数,则将其添加到子数组中。多数民众赞成。
var string = 'Management Portal!@!@Production Issue Handling!@!@/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!@!@ Event Browser!@!@/IONSWeb/orderManagement/eventBrowser.do, Management Portal!@!@ Order Workflow!@!@/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!@!@Admin Message!@!@/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!@!@Audit!@!@/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!@!@Locks!@!@/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!@!@Queue!@!@/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!@!@Role!@!@/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!@!@Routing Rule!@!@/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!@!@Task Code!@!@/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!@!@Trigger OutEvent!@!@/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!@!@User!@!@/IONSWeb/userManagement/begin.do, ADMINISTRATION!@!@Refresh Application Cache!@!@/IONSWeb/userManagement/refreshApplnCache.do';
var pieces = string.split('!@!@');
var first = pieces[0];
//Get out the first one, that will be the key
pieces.shift();
//Create the object
var object = {
'name': first,
'sub': []
};
//Iterate through elements
var i = 0;
var sub = [];
$.each(pieces, function (idx, piece) {
if (i % 2 == 0) {
sub['name'] = piece;
} else {
sub['url'] = piece;
object.sub.push(sub);
}
i++;
});
console.log(object);
答案 1 :(得分:0)
尝试这样的事情:
小提琴:https://jsfiddle.net/ug85d7o7/6/
var jsonData = [],
item, name,
subItem, subUrl,
i, j,
a = [
"Management Portal!@!@Production Issue Handling!@!@/IONSWeb/refDataManagement/searchDynamicScripts.do",
"Management Portal!@!@ Event Browser!@!@/IONSWeb/orderManagement/eventBrowser.do",
"Management Portal!@!@ Order Workflow!@!@/IONSWeb/orderManagement/SearchOrdersWorkflow.do",
"ADMINISTRATION!@!@Admin Message!@!@/IONSWeb/userManagement/getMessageForBroadcast.do",
"ADMINISTRATION!@!@Audit!@!@/IONSWeb/userManagement/auditManagement.do",
"ADMINISTRATION!@!@Locks!@!@/IONSWeb/userManagement/lockSearch.do",
"ADMINISTRATION!@!@Queue!@!@/IONSWeb/GroupManagement/begin.do",
"ADMINISTRATION!@!@Role!@!@/IONSWeb/userManagement/goToRolePage.do",
"ADMINISTRATION!@!@Routing Rule!@!@/IONSWeb/ruleManagement/showRules.do",
"ADMINISTRATION!@!@Task Code!@!@/IONSWeb/ManageTaskCode/begin.do",
"ADMINISTRATION!@!@Trigger OutEvent!@!@/IONSWeb/triggerOutEvent.jsp",
"ADMINISTRATION!@!@User!@!@/IONSWeb/userManagement/begin.do",
"ADMINISTRATION!@!@Refresh Application Cache!@!@/IONSWeb/userManagement/refreshApplnCache.do"
];
for(i=0; i<a.length; i++)
{
item = a[i].split("!@!@");
name = item[0];
subName = item[1];
subUrl = item[2];
subItem = null;
for (j=0; j<jsonData.length; j++)
{
if (jsonData[j].Name == name)
{
subItem = jsonData[j].sub;
break;
}
}
if (!subItem)
{
jsonData.push({"Name" : name, "sub" : [] });
subItem = jsonData[jsonData.length-1].sub;
}
subItem.push({"Name" : subName, "url" : subUrl });
}
alert(JSON.stringify(jsonData));
结果:
[
{
"Name": "Management Portal",
"sub": [
{
"Name": "Production Issue Handling",
"url": "/IONSWeb/refDataManagement/searchDynamicScripts.do"
},
{
"Name": " Event Browser",
"url": "/IONSWeb/orderManagement/eventBrowser.do"
},
{
"Name": " Order Workflow",
"url": "/IONSWeb/orderManagement/SearchOrdersWorkflow.do"
}
]
},
{
"Name": "ADMINISTRATION",
"sub": [
{
"Name": "Admin Message",
"url": "/IONSWeb/userManagement/getMessageForBroadcast.do"
},
{
"Name": "Audit",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"Name": "Locks",
"url": "/IONSWeb/userManagement/lockSearch.do"
},
{
"Name": "Queue",
"url": "/IONSWeb/GroupManagement/begin.do"
},
{
"Name": "Role",
"url": "/IONSWeb/userManagement/goToRolePage.do"
},
{
"Name": "Routing Rule",
"url": "/IONSWeb/ruleManagement/showRules.do"
},
{
"Name": "Task Code",
"url": "/IONSWeb/ManageTaskCode/begin.do"
},
{
"Name": "Trigger OutEvent",
"url": "/IONSWeb/triggerOutEvent.jsp"
},
{
"Name": "User",
"url": "/IONSWeb/userManagement/begin.do"
},
{
"Name": "Refresh Application Cache",
"url": "/IONSWeb/userManagement/refreshApplnCache.do"
}
]
}
]