Since you want an exact string and the json you have actually is a number (which means 5e-0000006
will equal 5e-6
) I would suggest using regex:
string json = "{\"myfield\":5e-0000006}";
Regex regex = new Regex("(?<=:)[^}]+");
string result = regex.Match(json).Value;
Explanation:
(?<=:)
look behind for a colon (:
)
[^}]+
match any character not being a right curly brace (}
), one or more times.
That should give you the value as an exact string.
Update:
If you want to match based on the myfield
variable, you can expand the regex to contain that information:
string json = "{\"myfield\":5e-0000006}";
Regex regex = new Regex("(?<=\"myfield\":)[^}]+");
string result = regex.Match(json).Value;
Now you will only get the line where you have \"myfield\"
in front - in case you have many lines.
You can of course replace \"myfield\"
with a variable, like this:
string json = "{\"myfield\":5e-0000006}";
string myvar = "myfield";
Regex regex = new Regex("(?<=\"" + myvar + "\":)[^}]+");
string result = regex.Match(json).Value;