3

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();
2
  • 1. extract necessary information from html 2. construct object 3. convert to json Commented May 3, 2017 at 9:22
  • @LeiYang could you give me some help making these things, please? Commented May 3, 2017 at 9:23

5 Answers 5

1

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);
    
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

it doesn't work to return json...how can i extract the content from the html?
0

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.

Comments

0

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; } }
    }

Comments

-1

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.

1 Comment

i want to add in my json only the rows from a table in that html response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.