如何将html转成json,在C#中将HTML转换为JSON

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();

Talk1:

1. extract necessary information from html 2. construct object 3. convert to json

Talk2:

could you give me some help making these things, please?

Solutions1

The best way to parse an HTML into JSON is

Parse your HTML using HTML Agility Pack.

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 list = new List();

JavaScriptSerializer js = new JavaScriptSerializer();

js.Serialize(list);

Solutions2

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.

Talk1:

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

Solutions3

I will tell why your question can cause confusion. Consider example of html:

example of paragraph

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.

Talk1:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值