在C#中将HTML转换为JSON

3
我是一名有用的助手,可以为您翻译文本。
我有一个返回HTML的C#帖子。我的帖子检查组织名称并返回具有该名称的组织列表。我的问题是,该帖子返回整个页面的HTML,而我只想要组织列表。我认为我应该将其转换为JSON,还是有其他可能性?
方法:POST
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();

  1. 从HTML中提取必要的信息
  2. 构建对象
  3. 转换为JSON
- Lei Yang
@LeiYang,你能帮我做这些东西吗?谢谢。 - S.over17
5个回答

1
将HTML解析为JSON的最佳方法是:
  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);
    

0
我创建了一个函数,通过其他答案的帮助将HTML中的JSON转换为JSON。
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);


        }

在我的类文件中,我创建了一个名为Wpjson的类,并在其中放置了以下内容:
 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; } }
    }

0

没有直接将HTML转换为JSON的方法。自己制作一个工具吧!

然而,我发现最简单的方法是直接从浏览器中复制/粘贴HTML到Excel中,并进行一些操作以形成“表格”结构。

接下来使用Office Script将Excel转换为JSON。


0
根据终端点的不同,你可以尝试在请求中添加一个 Accept 头部,要求返回 JSON 格式的数据。
我不清楚 WebRequest 上可能设定了哪些属性,但可能是这样的。
request.Accept = "application/json";

以这种方式,请求会要求服务器以 JSON 格式返回结果,这可能更易于使用。如果失败了,那么您就必须从 HTML 中提取内容,构造对象,然后将该对象序列化为 JSON。

无法返回 JSON,请问我该如何从 HTML 中提取内容? - S.over17

0
我会告诉你为什么你的问题会引起混淆。考虑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是一种用于生成HTML或初始基础的东西。因此,当您说要将HTML转换为JSON时,这真的很令人困惑,因为无法确定您希望按照哪些规则进行此转换,或者在创建JSON时应忽略/添加哪些HTML标记。


我想在我的JSON中仅添加来自HTML响应中表格的行。 - S.over17

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接