S.over17
S.over17

Reputation: 209

Convert html to json in c#

I have a c# post which returns me html. My post is checking for an organization name and return the list of organizations with that name. My problem is that the post returns the html for whole page and i want only the list of organizations. I think that i should convert to json, or there is other possibility?

Post method:

WebRequest request = WebRequest.Create("http://www.mfinante.ro/numeCod.html");
// Set the Method property of the request to POST.
request.Method = "POST";

// Create POST data and convert it to a byte array.
string postData = "judet=40&name=ORACLE&submit=Vizualizare";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();

// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.

Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

Upvotes: 3

Views: 21338

Answers (5)

Spinstaz
Spinstaz

Reputation: 331

I created a function to convert the json from HTML to json thanks to the other answer.

public string convertHtmlToJson(string finalHtml, string title, bool status)
        {

            Wpjson jsonObject = new Wpjson();
            jsonObject.Title = title;
            jsonObject.Content = finalHtml;
            jsonObject.Status = status;

            return new JavaScriptSerializer().Serialize(jsonObject);


        }

In my class file I created I called it Wpjson and inside I put below:

 public class Wpjson
    {

        string title = string.Empty;
        string content = string.Empty;
        bool status = false;



        public string Title
        { get { return title; } set { title = value; } }

        public string Content
        { get { return content; } set { content = value; } }

        public bool Status
        { get { return status; } set { status = value; } }
    }

Upvotes: 0

Fandango68
Fandango68

Reputation: 4848

There is no direct way to convert HTML to JSON. Make a tool!

However, the easiest way I found was to copy/paste the HTML directly from a browser, into Excel, with some manipulation to form a "table" structure.

Next use an Office Script to convert from Excel to JSON.

Upvotes: 0

Therichpost
Therichpost

Reputation: 1815

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.

Upvotes: -1

Ankit
Ankit

Reputation: 34

The best way to parse an HTML into JSON is

  1. Parse your HTML using HTML Agility Pack.
  2. Depending on what is in your HTML you can create a class in c# and create an object of it OR you can create a list of objects if HTML content is repetitive.

    Class MyItem
    {
        int employeeId = 0;
        string employeeName = String.Empty;
    }
    
    List<MyItem> list = new List<MyItem>();
    JavaScriptSerializer js = new JavaScriptSerializer();
    js.Serialize(list);
    

Upvotes: 1

J.N.
J.N.

Reputation: 537

Depending on the endpoint, you might have luck adding an Accept header to the request, asking for JSON.

I do not what properties that might be set on a WebRequest, but it might be something like

request.Accept = "application/json";

In that way, the request will ask the server to return the result in a JSON format, which might be more usable for you. If it fails, then you'll have to extract the content from the HTML, constructing an object and then serialise that object into JSON.

Upvotes: 0

Related Questions