views:

20

answers:

0

Hello!

So, I am using the HtmlAgility pack (http://htmlagilitypack.codeplex.com/) to parse a script node and then I use regular expressions to parse out an object definition. The string I end up with is plain javascript that defines an object.

Here is the sample Javascript I am trying to parse:

<!--Module 328 Buying Options Table-->
<script type="text/javascript" language="JavaScript">
var DefaultItem = {
    itemId: 14562198,
    prodName: 'FIFA Soccer 11 (PS3)',
    brand: 'Electronic Arts',
    model: '19321',
    primaryCategoryPath: '0:2636:1040579',
    upc: '0001463319321',
    buttonHtml: '<input type="Image" name="add_to_cart" src="/i/style/def/btn/bp_cart_btn_giant_new.gif" alt="Add to Cart" id="onlineAdd2CartBtn">',
    isThresholdShippingEligible: false,
    thresholdShipProgramName: '',
    thresholdShippingAmount: 0,
    formatedThresholdShipAmount: '',
    isS2Sonly: false,
    isPUTEligible: false,
    isWalmartPrimarySeller: true,
    primarySellerId: 0,
    canAddtoCart: true,
    hasMatureContent: false,
    isHazMat: false,
    friendlyName: 'FIFA Soccer 11 (PS3)',
    itemClassId: 22,
    itemTypeId: 0,
    currentItemPrice: 56.99,
    contentRating: 'Everyone',
    slapFlag: 'N',
    isS2SEnabled: true,
    isS2HEnabled: true,
    isBuyableOnWWW: true,
    isBuyableInStore: true,
    isEmailMe: false,
    isInStock: true,
    isInStore: true,
    sellers: [{
        sellerId: 0,
        sellerName: 'Walmart.com',
        minPrice: 56.99,
        maxPrice: 56.99,
        canAddtoCart: true,
        price: '...',
        priceFlags: '...',
        merchFlags: '',
        deliveryOptions: '...',
        isDisplayable: true,
        isComingSoon: false,
        isPreOrder: false,
        isPreOrderOOS: false,
        isRunout: false
    }],
    attributeData: []
};

// more Javascript I don't care about...
// ...
</script>

In this case, I want to parse out the content between var DefaultItem = and the semi-colon(;) after the closing bracket (}). After that, I want to convert that into JSON and then deserialize into an object.

Question: How do I convert the parsed string to JSON? Once I have valid JSON, I can easily deserialize to an object using JSON.Net.

Thank you! Hope my question makes sense.