Reputation: 2369
I am using the following code adapted from this so answer
// remove outer bracket
JSONdata = JSONdata.Trim().Trim('[', ']');
// remove white space and line breaks except between double qoutes
JSONdata = Regex.Replace(JSONdata.Trim('"').Replace("\\\"", "\""), "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");
// add back bracket
JSONdata = "[" + JSONdata + "]";
Then I get data like this:
[
[
{"OptionChoice":14151,"OptionText":"Television"},
{"OptionChoice":14755,"OptionText":"Test[ something ]"}
]
,{"OptionChoice":361,"OptionText":"Yes"}
]
OR Minified (as it is in my real code)
[[{"OptionChoice":14151,"OptionText":"Television"},{"OptionChoice":14755,"OptionText":"Test[ something ]"}],{"OptionChoice":361,"OptionText":"Yes"}]
Whitespace and line breaks added back for example only. Real data has no whitespace or line breaks
I would like to be able to get keep the outer brackets but remove the set of inner brackets that is not in double quotes.
EDIT: Expected output
[
{"OptionChoice":14151,"OptionText":"Television"},
{"OptionChoice":14755,"OptionText":"Test[ something ]"},
{"OptionChoice":361,"OptionText":"Yes"}
]
Minified:
[{"OptionChoice":14151,"OptionText":"Television"},{"OptionChoice":14755,"OptionText":"Test[ something ]"},{"OptionChoice":361,"OptionText":"Yes"}]
I must confess that I don't fully understand the regexp that I have used.
… and now I have two problems.
Upvotes: 0
Views: 3106
Reputation: 67968
(?!^)\[(?=(?:[^"]*"[^"]*")*[^"]*$)|\](?!$)(?=(?:[^"]*"[^"]*")*[^"]*$)
Try this.See demo.Replace by ``.Do not forget the flags.
http://regex101.com/r/zR2tR4/25
Upvotes: 1