c# - Convert key=value string to JSON -
i have string of keys , values in following format:
key1=somevalue, key2="hello, world!", some.other.key=hello world!, key4="hello, ""world""!" how can convert json string using c#? can done using regex, can't come right pattern. neither able figure out how using libraries newtonsoft.json.
the json want produce following:
{ "key1":"somevalue", "key2":"hello, world!", "some.other.key":"hello world!", "key4":"hello, \"world\"!" }
well, nested quotes updated question things go match trickier. can't see viable way of extracting values arbitrary level of nested quotes. (this true regex approach -- it's still possible scan string manually , count number of subsequent quotes respect nesting level.)
assuming limit ourselves 1 level of nested quoted strings, regex be:
(?<key>[^=,\s]+)=(?:"(?<value>(?:[^"]|""[^"]*"")*?)"|(?<value>[^,]*))(?:,|$) then can find matches , reformat pairs according json rules:
var input = @"key1=somevalue, key2=""hello, world!"", some.other.key=hello ""world""!, key4=""hello, """"world""""!"", key5=""hello, """"world""""!"", key6=""""""hello"""", """"world""""!"""; var pairs = regex.matches(input, @"(?<key>[^=,\s]+)=(?:""(?<value>(?:[^""]|""""[^""]*"""")*?)""|(?<value>[^,]*))(?:,|$)") .cast<match>() .select(m => string.format(" {0}: {1}", jsonconvert.tostring(m.groups["key"].value), jsonconvert.tostring(m.groups["value"].value.replace("\"\"", "\"")))); var json = "{\n" + string.join(",\n", pairs) + "\n}"; regex explanation:
(?<key>- start capture group namedkey[^=,\s]+- match non-empty sequence of characters excluding=,,, , whitespaces)- endkeygroup
=- match equals sign literally(?:- start unnamed group used group alternatives- the first alternative - quoted value:
"- literal opening quote(?<value>- start capture group namedvalue(?:[^"]|""[^"]*"")*- match sequence of non-quotes or quoted string (please not quotes doubled)?- make previous match non-greedy)- endvaluegroup
"- literal closing quote|- alternatives delimiter- the second alternative - unquoted value:
(?<value>- startvaluecapture group - .net regex flavour maintans stack of named groups can access either of alternative capture groups name[^,]*- match sequence not containing commas)- end secondvaluegroup
)- end unnamed group
(?:,|$)- match either comma or end of string (both expected finish value)
Comments
Post a Comment