0

Is there an elegant way to get lat and lon values from this string using C#? Thanks. String is like this :

<input type="hidden" name="myinput" id="myinput" value='{"lat":11.111111,"lon":22.222222}'>
jason
  • 6,962
  • 36
  • 117
  • 198
  • 2
    Depends on your definition of elegant. Immediately my first though is just use a Regex. – Brandon Nov 18 '16 at 14:42
  • @Brandon [Regex why not](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Steve Nov 18 '16 at 14:44
  • @Steve Sure. He's not matching tags though. He could overkill the everliving out of it and use a parser as mentioned in your link. 6 in one..... – Brandon Nov 18 '16 at 14:46
  • Can someone say how I can parse it with code? Thanks. – jason Nov 18 '16 at 14:47
  • Try HtmlAgilityPack, see this SO link [HtmlAgilityPack](http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack) – Steve Nov 18 '16 at 15:02

1 Answers1

2

Per my comment, this is how I'd do it. I'm not claiming to be awesome at Regex by any means. There's probably a better way.

var r = new Regex(@"""lat"":(?<lat>\d+\.\d+),""lon"":(?<lon>\d+\.\d+)");
var m = r.Match(@"<input type=""hidden"" name=""myinput"" id=""myinput"" value='{""lat"":11.111111,""lon"":22.222222}'>");
if ( m.Success )
{
    Console.WriteLine(f.Groups["lat"]);
    Console.WriteLine(f.Groups["lon"]);
}
Brandon
  • 4,491
  • 6
  • 38
  • 59