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 named key
    • [^=,\s]+ - match non-empty sequence of characters excluding =, ,, , whitespaces
    • ) - end key group
  • = - match equals sign literally
  • (?: - start unnamed group used group alternatives
    • the first alternative - quoted value:
    • " - literal opening quote
    • (?<value> - start capture group named value
      • (?:[^"]|""[^"]*"")* - match sequence of non-quotes or quoted string (please not quotes doubled)
      • ? - make previous match non-greedy
      • ) - end value group
    • " - literal closing quote
    • | - alternatives delimiter
    • the second alternative - unquoted value:
    • (?<value> - start value capture group - .net regex flavour maintans stack of named groups can access either of alternative capture groups name
      • [^,]* - match sequence not containing commas
      • ) - end second value group
    • ) - end unnamed group
  • (?:,|$) - match either comma or end of string (both expected finish value)

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -