c# - Parse HTML Code to get a JSON property out -
this question has answer here:
- what best way parse html in c#? [closed] 15 answers
hi have following code c#.net
asciiencoding encoding = new asciiencoding(); string postdata = "membershipcardnumber=" + cardnumber; postdata += ("&terminalname=" + terminalname); postdata += ("&serviceid=" + casinoid); byte[] data = encoding.getbytes(postdata); httpwebrequest myrequest = (httpwebrequest)webrequest.create("http://myurl"); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.contentlength = data.length; stream newstream = myrequest.getrequeststream(); newstream.write(data, 0, data.length); httpwebresponse response = (httpwebresponse)myrequest.getresponse(); // stream associated response. stream receivestream = response.getresponsestream(); // pipes stream higher level stream reader required encoding format. streamreader readstream = new streamreader(receivestream, encoding.utf8); console.writeline("response stream received."); var egm_response = readstream.readtoend(); console.writeline(egm_response);
the variable egm_response
outputs entire html code. part of it:
<div class="result" style="margin-left: 20px;"> <p>json result :</p> {"createegmsession":{"isstarted":0,"datecreated":"","transactionmessage":"terminal has active session.","errorcode":37}} </div> <div class="clear"></div>
how can or parse html , value after errorcode
?
first, service should return json, not combination of html , json. if json, parse json response .net object using json.net, can ask errorcode
property.
second, use regex match errorcode
:
"errorcode"\:\s+(\d+)
i advise go option 1 if possible, since make life lot easier.