且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RegEx将XML转换为JSON

更新时间:2023-02-04 09:00:56

好的,请注意,这是一个快速修复程序,但是似乎可以正常工作.这只会添加一个数组结构,因此您的键不会有好几次(但不会破坏该键).
更改:

Ok so, note that it's a quick fix but nevertheless it seems to work. This will just ADD an array structure so your won't have several times the same key (but it won't destroy that key).
Change:

replace(/< *(\w[\w-]+\b) *>(?=.*?< \/\1\},\{)/g, '"$1":[{').
split(/\},\{/).
reverse().
join('},{').
replace(/< *\/ *(\w[\w-]+\b) *>(?=.*?"\1":\[\{)/g, '}],').
split(/\},\{/).
reverse().
join('},{').

这是尝试实现数组的尝试.
然后放:

which is an attempt to implement arrays.
And put :

replace(/< *(\w[\w-]+\b) *>(?=("\w[\w-]+\b)":\{.*?\},\2)(.*?)< *\/ *\1 *>/, '"$1":[$3],')

请注意,我几乎使用了他的匹配方式.至少看来这对您有用.

Note that I used pretty much his way of matching things. That seemed to work for you example at least.