How to convert html to json with C#? - html

My query is that I want to convert html to json with C#. Is there any way to do it. I searched a lot and found articles related to using Javascript Serializer and Newtonsoft to serialize the html string to json. But these serializers do nothing except adding a opening and closing curly braces around the html string. I don't want that. I want to convert whole html to json so that I can get relevant information from the html using C# objects instead of parsing html with regular exressions. Html can be any valid html from any website available on the internet. I am getting the html using http request & response objects using C#.
Please don't suggest using html agility pack because that will also do the same thing that Serialization does.
If anybody have any idea how to do this with C# then please share your ideas.

I will tell why your question can cause confusion.
Consider example of html:
<html>
<body>
<p> example of paragraph </p>
</body>
</html>
Example of json:
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
json is something, on which html is generated or initial fundament. So when you say you want to convert html to json it's really confusing, because it is impossible to figure out according to which rules you want to make this conversion. Or which tags from html should be ignored/added while creating json.

There is a javascript example of the solution here: Map HTML to JSON
DOM Parsers are pretty similar so you can try implementing it in C#. (I'd be interested in such implementation as well :D )

Related

Decode all html entities on HTML document

I'm consuming an API that returns encoded HTML entities on the response (I don't have access to the code).
This response contains text that will be later used on the HTML, such as inside
<p>, <h1> ...
I would like to know the best way to decode all the HTML entities on the document.
All the solutions I found tell me to decode each API response. But it isn't scalable on my case.
Btw, I'm using React.

v-html doesn't output HTML properly

I make use of an API that consists of a JSON with prefetched data from a CMS. The partial JSON looks like this:
"content": "<p><em>We're looking for a pro-active, analytical, commercially minded and ambitious deal-closer who loves to work - and play - hard to join our Company.
I then pass this data to a child component and then render it by using v-html. I expected this to output the HTML tags with styling and semantics. However, it renders the HTML tags as plain text:
<p><em/>We're looking for a pro-active, analytical, commercially minded and ambitious deal-closer who loves to work - and play - hard to join our Company.
Does anyone know what I am doing wrong? Should I have parsed the JSON? Should I have decoded the raw JSON to HTML tags first?
Nothing to do with JSON; everything to do with your web service unhelpfully giving you unparsed HTML.
You're going to have to decode these HTML entities yourself.
One common trick is to feed the unparsed HTML to an off-DOM element, then read it back via textContent, which will give you the parsed version.
let p = document.createElement('p');
p.innerHTML ='<p>'
console.log(p.textContent); //"<p>"

JSON: Display JSON String on HTML website

I would like my webpage to display what resembles JASON Parser
does (right panel). I first installed JSONView on my Chrome.
Then I composed a string {"isbn":"asdf","name":"qwer","price":1} which I fed to JSON Parser and made sure it indeed obeyed JSON format.
Eventually, I attempted to display it on my webpage by doing:
out.println("{\"isbn\":\"asdf\",\"name\":\"qwer\",\"price\":1}\");
but it was simply displayed like any other normal String instead of being formatted like this.
How do I manage to make my webpage automatically display JSON formatted?
Thanks!
Edit: I didn't realise this was JSP. The answer I'm giving relates to using the JavaScript method JSON.stringify() which you'd somehow achieve within client-side JavaScript within your JSP project.
Use JSON.stringify() to 'pretty-print' the original JavaScript object.
For example, to have two spaces of indentation:
var str = JSON.stringify(obj, null, 2);
If this has to be achieved within Java code then you could use the GSON library. Example: Pretty-Print JSON in Java

Objective-C event-driven HTML parsing

I need to be able to parse HTML snippets in an event-driven way. For example, if the parser finds a HTML tag, it should notify me and pass the HTML tag, value, attributes etc. to a delegate. I cannot use NSXMLParser because I have messy HTML. Is there a useful library for that?
What I want to do is parse the HTML and create a NSAttributedArray and display it in a UITextView.
YES you can parse HTML content of file.
If you want to get specific value from HTML content you need to Parce HTML content by using Hpple. Also This is documentation with exmple that are is for parse HTML. Another way is rexeg but it is more complicated so this is best way in your case.

How to parse these html string in objective c

I'm getting an string from json response. In the string there are HTML tags,
<ul class='video-list-container'><li><a href='?page_id=6602&playid=6'>مكتبة الفيديو</a></li><li><a href='?page_id=6602&playid=10'>الزكاة</a></li><li><a href='?page_id=6602&playid=11'>الصلاة</a></li></ul>\n
Now all I want to do is to do is, get the arabic string with the href link and display in the UITableviewcell. And when selected row should move to next viewcontroller.
How can do this. I'm struck in parsing the string.
Can anyone help me.
Thanks In advance.
I think this tutorials might help you ,
AFNetworking is smart enough to load and process structured data over the network, as well as plain old HTTP requests. In particular, it supports JSON, XML and Property Lists (plists).
you can follow the tutorial given below for more clarification
http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial
You should try using the library "hpple" which is a wrapper around "libxml2". You can find hpple here.
Hpple allows you to parse HTML in Obj-C using XPath expressions. With XPath selectors you will be able to select what content you want to extract out of your HTML. You can read and learn more about XPath here.
Hope it helps. :)
Cheers!