想要在循环

时间:2018-01-05 09:22:50

标签: arrays json xml parsing key

我有一个json格式。现在我想从我的json中替换所有 p:并将其转换为真实名称,如p:OrderId to OrderId,p:Firstname to Firstname。

请查看下面的JSON。

  

{ “状态”: “成功”, “MSG”:{ “$”:{ “的xmlns:P”: “342”, “的xmlns:的xsi”:“测试   链接 “ ”的xsi:的schemaLocation“: ”324“, ”源“: ”234234“}, ”P:客户“:[{ ”P:的OrderId“:[ ”asdasd“], ”P:姓“:[”英格丽 “],” p:名字 “:[” 布莱尼 “],” p:EmailAddress的 “:[” asd@ad.com “],” p:出生日期 “:[” 1965年9月23日 “],” p :CellularPhone “:” 123465798 “],” p:城市 “:” ASD “],” p:国家 “:” asdda “],” p:POSTALCODE “:” 23332 “],” p:的StreetAddress “:[” ASD “],” p:PARTNERID “:[” 1-84OPG “],” p:CommunicationPreferences “:[{” 号码:电话 “:[” 12 “],” p:电子邮件 “:[” 123 “],” p:邮件 “:[” 231 “],” p:SMS “:[” 内 “]}],” p:ServiceAgreement “:[{” p:AgreementType “:[” Bredbånd    - Privat“],”p:AgreementStartDate“:[”123“],”p:AgreementStatus“:[”ASS“],”p:Comment“:[”\ n \ nse。   \ n \ n “个],” P:ListOfAssets “:[{” P:资产 “:[{” P:ProductPartNumber “:[” 234 “],” P:评论 “:[” “]},{” P :ProductPartNumber “:[” 123 “],” p:评论 “:[” “]},{” 号码:ProductPartNumber “:[” 12313 “],” p:评论 “:[” “]},{” p :ProductPartNumber “:[” 123 “],” p:评论 “:[” “]},{” 号码:ProductPartNumber “:[” 123 “],” p:评论 “:[” “]},{” p :ProductPartNumber “:[” 123123 “],” p:评论 “:[”“]}]}]}]}]}}

输出:https://prnt.sc/hwc157

注意:我已将实际值转换为演示值。我需要用值更改键名。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:



var jsn = '{"status":"success","msg":{"$":{"xmlns:p":"342","xmlns:xsi":"test link","xsi:schemaLocation":"324","source":"234234"},"p:Customer":[{"p:OrderId":["asdasd"],"p:FirstName":["Ingrid"],"p:LastName":["Bryne"],"p:EmailAddress":["asd@ad.com"],"p:BirthDate":["1965-09-23"],"p:CellularPhone":["123465798"],"p:City":["asd"],"p:Country":["asdda"],"p:PostalCode":["23332"],"p:StreetAddress":["asd"],"p:PartnerId":["1-84OPG"],"p:CommunicationPreferences":[{"p:Phone":["12"],"p:Email":["123"],"p:Mail":["231"],"p:SMS":["Nei"]}],"p:ServiceAgreement":[{"p:AgreementType":["Bredbånd - Privat"],"p:AgreementStartDate":["123"],"p:AgreementStatus":["ASS"],"p:Comment":["se."],"p:ListOfAssets":[{"p:Asset":[{"p:ProductPartNumber":["234"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["12313"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123123"],"p:Comments":[""]}]}]}]}]}}';

	var ret = jsn.replace(/p:/g,'');
	var obj = JSON.parse(ret);
	console.log(obj);