Reading JSON data to C# string

c# html-agility-pack json json.net xml

Question

I'm working on a C# Console App project where I need to extract certain values from some JSON data that was parsed from a website, such the product price and color.

My JSON data, obtained by means of HTMLAgilityPack from a website. I had to swap out\" with " Another question is, how can C# handle this appropriately to make it legitimate JSON?

{
    "currentAsinData": {
        "Asin": "B0013NCYX4",
        "buyingPPU": "",
        "variantImages": [
            {
                "tinyImage": {
                    "HEIGHT": "70",
                    "URL": "http: //ecx.images-amazon.com/images/I/419CBUN6h8L._SL110_.jpg",
                    "WIDTH": "110"
                },
                "swatchImage": {
                    "HEIGHT": "19",
                    "URL": "http: //ecx.images-amazon.com/images/I/419CBUN6h8L._SL30_.jpg",
                    "WIDTH": "30"
                },
                "mediumImage": {
                    "HEIGHT": "168",
                    "URL": "http: //ecx.images-amazon.com/images/I/419CBUN6h8L._SX168_.jpg",
                    "WIDTH": "168"
                },
                "largeImage": {
                    "HEIGHT": "270",
                    "URL": "http: //ecx.images-amazon.com/images/I/419CBUN6h8L._SX270_.jpg",
                    "WIDTH": "270"
                },
                "thumbnailImage": {
                    "HEIGHT": "120",
                    "URL": "http: //ecx.images-amazon.com/images/I/419CBUN6h8L._SX120_.jpg",
                    "WIDTH": "120"
                }
            }
        ]
    }
}

Now, the JSON above is valid as far as I can tell, but C# prevents me from reading the data." Then if I use\" my JArray unsuccessfully deserializes the object

My ultimate objective is to hopefully interpret the JSON so that I can obtain the data and convert it to a C# string for future use. I'm new to JSON in C# and I'm using the JSON.NET package. But I'm at a loss about how to proceed.

I appreciate it in advance.

more details as required.

Here is the code I used to scrape the javascript JSON data.

string theScript = xd.SelectSingleNode(".//div[contains(@class,'webstore-ProductJSONData')]/script[contains(.,'var detailData')]").GetInnerXML().HtmlDecode();
        if(theScript != null)
        { 
            string[] varsln = Regex.Split(theScript, "var detailData =");
            string json = varsln[1].HtmlDecode().Replace("};\nvar extensibilityData = {};\n\r\n//]]>//", "").Trim();

            Console.WriteLine(json);
        }

The webpage From where I'm taking the JSON

http://www.dangleberrymusic.co.uk/Childrens-Childs-Electric-Guitar-  quarter/dp/B00ESEOXWK?class=quickView&field_availability=-1&field_browse=1592919031&id=Childrens+Childs+Electric+Guitar+quarter&ie=UTF8&refinementHistory=color_map%2Cbrandtextbin%2Csubjectbin%2Cprice%2Csize_name&searchNodeID=1592919031&searchPage=1&searchRank=salesrank&searchSize=12
1
0
3/11/2014 1:59:50 PM

Popular Answer

I believe that the data's trailing brace was removed by your javascript scraping code, which prevented JSON.net from successfully parsing the data as JSON. You possess this:

 .Replace("};\nvar extensibilityData = {};\n\r\n//]]>//", "")

But it should be as follows:

 .Replace(";\nvar extensibilityData = {};\n\r\n//]]>//", "")

Once you have a valid JSON string, you may deserialize it using the following method:

JToken token = JToken.Parse(json);  // works with either objects or arrays

You may then utilize Json.API LINQ to JSON Net's to get the desired data from theJToken . Zzz-30-Zzz in the documentation demonstrates how to do a search for certain values.

0
3/11/2014 3:12:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow