1

Is there a way I could manipulate a string containing:

{"Correct":true,"Explanation":{"TextWithHtml":null},"ImageFile":null,"Response":false,"Text":{"TextWithHtml":"1 -1 4 -16"}},{"Correct":true,"Explanation":{"TextWithHtml":null},"ImageFile":null,"Response":false,"Text":{"TextWithHtml":"1 -1 4 2147483644"}}]

and remove the {"TextWithHtml": and } but leave the inside contents?

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • I hope this So could help ,http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp – dreamweiver Jun 25 '13 at 11:59
  • 1
    What you're asking is for a regular expression that will match any valid JSON string value. Why reinvent the wheel? Use a JSON parser to parse the it into a form you can manipulate, and then use a JSON serializer to write it back in the format you want. – Jim Mischel Jun 25 '13 at 13:52

3 Answers3

2

IMHO, you should create a class

public class YourClassName
{
    public string TextWithHtml{ get; set; }
}

And modify your answer class

public class Answer : AuditableTable
{
    public bool Correct { get; set; }
    public bool Response { get; set; }
    public YourClassName Text { get; set; }
    public string ImageFile { get; set; }
    public YourClassName Explanation { get; set; }
}
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • If I could just find a way to change the characters in the source string it would work but I am not sure how to do a regular expression for making the changes. – Alan2 Jun 25 '13 at 13:02
0

Try this:

string sSource = @"{"Correct":true,"Explanation":{"TextWithHtml":null},"ImageFile":null,"Response":false,"Text":{"TextWithHtml":"1
    -1 4 -16"}},{"Correct":true,"Explanation":{"TextWithHtml":null},"ImageFile":null,"Response":false,"Text":{"TextWithHtml":"1
    -1 4 2147483644"}}]"; 

string pattern = @"({"TextWithHtml":)|(?(?<=({"TextWithHtml":)[^}]*)})"; 

string sResult= Regex.Replace(@sSource, @pattern, "");

some explanation:

remove

{"TextWithHtml":

or remove

}

if it is preceeded by:

{"TextWithHtml": plus some characters that aren't '{'

EDIT:

for {} brackets inside a TextWithHtml string:

({"TextWithHtml":)|(?(?<=(({"TextWithHtml":)(("[^"]*")|null)))})

There is still an issue with escaped quote characters (\") though.

Arie
  • 5,251
  • 2
  • 33
  • 54
  • What's that going to do with something like `{"TextWithHtml":"This is a string with {{{ }}} } stuff in it"}`? – Jim Mischel Jun 25 '13 at 14:05
  • "This is a string with {{{ }} } stuff in it"} - I don't know if that's wat you want, it may need some improvements, on your original string result is like this: {"Correct":true,"Explanation":null,"ImageFile":null,"Response":false,"Text":"1 -1 4 -16"},{"Correct":true,"Explanation" null,"ImageFile":null,"Response":false,"Text":"1 -1 4 2147483644"}] – Arie Jun 25 '13 at 14:14
  • My question is what happens if there is a `}` character inside the text that you're trying to keep? It looks to me like your expression will stop matching at that `}`, and not get the entire string. – Jim Mischel Jun 25 '13 at 16:01
  • @Gemma: This solution will fail if the text contains a `}` character. For example, given the string `"{\"TextWithHtml\":\"This } is bad\"}"`, the result is `"This is bad"}`. Note that the embedded `}` is removed and the trailing `}` is kept. – Jim Mischel Jun 26 '13 at 19:33
0

To match all the data you want, use the following regex:

{"TextWithHtml":.*?}

Use Regex.Replace(...) to replace the matches with the substring.

string data = match.Value.Substring(16, match.Value.Length-16-1);

Next, trim the data if it contains the " marks:

data.Trim('"');
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80