使用正则表达式将字符串数据的键和值格式化为json键值对

时间:2016-01-28 20:42:35

标签: java json regex string

以下是我们必须用双引号括起来的简单数据" Key":" value"格式化:

  

{Data:{Management:[{individual:{individual_Suffx:,individual_FName:XYZ,individual_LName:ABC,individual_Emplyee_Title:BOARD SECRETARY& PRESIDENT / CEO,individual_Directng_MName:MNO,individual_DOB:1960-05-21},individual_Tin:{ },individual_NPI_Id:{},individual_OrgIndvdl:[{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:W},{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:10},{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:15 }],Mngng_Mdcr:{}},{Mngng_Indvdl:{Mngng_Indvdl_FName:TIMOTHY,Mngng_Indvdl_LName:托雷,Mngng_Indvdl_MName:C,Mngng_Indvdl_DOB:1958年7月2日},Mngng_Tin:{},Mngng_NPI_Id:{},Mngng_OrgIndvdl:{OwnrshpIntrst_MngngCntrl_EfctvDt:2014 -05-01,Ownrshp_MngngCntrl_RoleCd:W},Mngng_Mdcr:{}}}}

1 个答案:

答案 0 :(得分:1)

不确定这是否涵盖所有极端情况,但此正则表达式适用于您的示例:

String json = ...
json = json.replaceAll("[^{}\\[\\]:,]+", "\"$0\"");