0x49D1 0x49D1 - 1 year ago 107
JSON Question

Extract JSON from string in .NET

The input string is mix of some text with valid JSON:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<META http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="Content-language" CONTENT="en">
<META HTTP-EQUIV="keywords" CONTENT="search words">

<script SRC="include/datepicker.js" LANGUAGE="JavaScript" TYPE="text/javascript"></script>
<script SRC="include/jsfunctions.js" LANGUAGE="JavaScript" TYPE="text/javascript"></script>

<link REL="stylesheet" TYPE="text/css" HREF="css/datepicker.css">

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;

The task is to deserialize the JSON part of it into some object. The string can begin with some text, but it surely contains the valid JSON. I've tried to use JSON validation REGEX, but there was a problem parsing such pattern in .NET.

So in the end I'd wanted to get only:

"List": [{
"ID": "175114",
"Number": "28992"

Clarification 1:

There is only single JSON object in whole the messy string, but the text can contain {}(its actually HTML and can contain javascripts with
<script> function(){.....

Answer Source

Use regex to find all possible JSON structures:

Regex example

Then iterate all these matches unitil you find a match that will not cause and exception:


If you know the format of the JSON structure, use JsonSchema.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download