0

One of services we are loading is responding just with pure JSON object. We are loading all services with html agility pack, all but this. Other services are rendering a script tag, with a JSON within, and it works as expected. But I am not able to load this data, when it comes in non-html format. Loaded document has no elements, and Text property is an empty string, DocumentElement's outer/inner html throws object null exception, innerText is empty or null.

I try to load this one service with HttpWebRequest and it does the job, but I don't want to mix technologies just because this service.

Is it possible load pure JSON data page with HtmlAgilityPack?

sasjaq
  • 761
  • 1
  • 11
  • 31
  • [How to read JavaScript object with XPath/HtmlAgilityPack](http://stackoverflow.com/questions/17740821/how-to-read-javascript-object-with-xpath-htmlagilitypack) – Eminem Feb 13 '16 at 12:48
  • I don't want to parse object, I want to get it. I'm using a NewtonsoftJson for parsing, thats not a question. – sasjaq Feb 13 '16 at 12:50
  • "Since the HTML Agility Pack doesn't evaluate any of the contents of the HTML, the javascript code should just be considered plain text. Use the SelectSingleNode method to find the piece of Javascript, then just grab the InnerHtml to get to the contents." – Eminem Feb 13 '16 at 12:51
  • The plain text is all I need to have. Once and again, I dont wan't to parse the json, I only need to get a plain text source of it. Json is parsed in other part of aplication, I need to read the non-html source. I will try that SelectSingleNode when I'm reaching my pc, but childElements collection of doc is an empty array, whether theres no element in page source. Sorry for mistakes, I'm writing on my mobile :) – sasjaq Feb 13 '16 at 14:04

1 Answers1

1

UPDATE :

Turned out that I've misunderstood the question.

Core functionality of HAP is for parsing HTML, while your problem is in downloading the HTML (or JSON in this case). HAP's HtmlWeb only provide basic functionality to perform this task, so you're very likely have to switch to other tools once you find yourself in a situation where HtmlWeb is no longer working. This is another example of this kind of situation : HTML Agility Pack settings


Initial Answer :

Quick test shows that DocumentElement.InnerText returns the JSON just fine :

var json = @"{
    identifier: '2051189775',     //PRODUCT ID
    fn: 'Fit- Whiskered Dark Wash Skirt',
    category: ['sale'],
    brand: 'Brand Name',
    price: '22.90',  // this would be the discount price
    amount: '31.80',  // this would be the original price
    currency: 'USD',
    //List can me even more.
};";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(json);

Console.WriteLine(doc.DocumentNode.InnerText);

Live demo here : https://dotnetfiddle.net/nPT49L

If this is not working for you, please post sample JSON data that will demonstrate the problem.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137