Shopfiy 富文本转JSON

Shopify的Customer Data新增的富文本类型,开始做API对接时,本以为会很简单,就和描述一样,将html代码编码后直接上传到接口.但是事情没有这么简单…
1.首先这个富文本很奇怪,支持的标签不多在这里插入图片描述

2.接口获取到数据是JSON类型

{"type":"root","children":[{"type":"heading","children":[{"type":"text","value":"text"}],"level":1}]}

3.继续查询接口文档和官方社区,得出结论:Customer Data只支持h标签,p标签,加粗,斜体,a标签,有序列表和无序列表.并且调用接口要按照官方的数据接口上传JSON字符串

下面直接开始放代码:

主程序

using System.Text.RegularExpressions;
using HtmlAgilityPack;

 static async Task Main(string[] args)
 {
     string htmlString = "<h1>标题 1</h1><h2>标题 2</h2><h3>标题 3</h3><h4>标题 4</h4><h5>标题 5</h5><h6>标题 6</h6><p>这是一段话。这是<strong>粗体文本</strong>。这是<em>斜体文本</em>。这是<em><strong>粗斜体文本</strong></em>。这是一个<a href=\"https://example.com/\" target=\"_blank\">链接。</a></p><ul><li>无序列表中的第一个</li><li>无序列表中的第二个</li></ul><p>中间有一段</p><ol><li>有序列表中的第一个</li><li>有序列表中的第二个</li></ol>";
     string jsonResult = ShopifyHtmlToJson(htmlString);
     Console.WriteLine(jsonResult);
     string strHtml = ShopifyJsonToHtml(jsonResult);
     Console.ReadLine();
 }

将Html转为JSON

        public static string ShopifyHtmlToJson(string html)
        {
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(html);
            RichTextResponse rootNode = new RichTextResponse
            {
                type = "root",
                children = new List<Child>()
            };
            
            var firstNodeList = htmlDoc.DocumentNode.ChildNodes;

            foreach (var firstNode in firstNodeList)
            {
                Child child = new Child();
                rootNode.children.Add(child);
                switch (firstNode.Name.ToLower())
                {
                    case "h1":
                        child.type = "heading";
                        child.level = 1;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "h2":
                        child.type = "heading";
                        child.level = 2;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "h3":
                        child.type = "heading";
                        child.level = 3;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "h4":
                        child.type = "heading";
                        child.level = 4;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "h5":
                        child.type = "heading";
                        child.level = 5;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "h6":
                        child.type = "heading";
                        child.level = 6;
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "p":
                        child.type = "paragraph";
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "ul":
                        child.type = "list";
                        child.listType = "unordered";
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                    case "ol":
                        child.type = "list";
                        child.listType = "ordered";
                        child.children = new List<Child>();
                        if (firstNode.HasChildNodes)
                        {
                            foreach (var secondNode in firstNode.ChildNodes)
                            {
                                child.children.Add(GetChildJson(secondNode));
                            }
                        }
                        else
                        {
                            child.children.Add(new Child
                            {
                                type = "text",
                                value = firstNode.InnerHtml
                            });
                        }
                        break;
                }

            }

            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Ignore,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };
            return JsonConvert.SerializeObject(rootNode, settings);
        }

        public static Child GetChildJson(HtmlNode htmlNode)
        {
            Child child = new Child();
            switch (htmlNode.Name.ToLower())
            {
                case "h1":
                    child.type = "  ";
                    child.level = 1;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "h2":
                    child.type = "heading";
                    child.level = 2;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "h3":
                    child.type = "heading";
                    child.level = 3;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "h4":
                    child.type = "heading";
                    child.level = 4;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "h5":
                    child.type = "heading";
                    child.level = 5;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "h6":
                    child.type = "heading";
                    child.level = 6;
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "p":
                    child.type = "paragraph";
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "ul":
                    child.type = "list";
                    child.listType = "unordered";
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "ol":
                    child.type = "list";
                    child.listType = "ordered";
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "li":
                    child.type = "list-item";
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                case "strong":
                    child.type = "text";
                    child.bold = true;
                    child.value = htmlNode.InnerHtml;
                    foreach (var firstNode in htmlNode.ChildNodes)
                    {
                        if (firstNode.Name.ToLower() == "em")
                        {
                            child.italic = true;
                            child.value = child.value.Replace("<em>", "");
                            child.value = child.value.Replace("</em>", "");
                        }
                    }
                    break;
                case "em":
                    child.type = "text";
                    child.italic = true;
                    child.value = htmlNode.InnerHtml;
                    foreach (var firstNode in htmlNode.ChildNodes)
                    {
                        if (firstNode.Name.ToLower() == "strong")
                        {
                            child.bold = true;
                            child.value = child.value.Replace("<strong>", "");
                            child.value = child.value.Replace("</strong>", "");
                        }
                    }
                    break;
                case "a":
                    child.type = "link";
                    child.target = htmlNode.GetAttributeValue("target", null);
                    child.url = htmlNode.GetAttributeValue("href", null);
                    child.title = htmlNode.GetAttributeValue("title", null);
                    child.children = new List<Child>();
                    if (htmlNode.HasChildNodes)
                    {
                        foreach (var firstNode in htmlNode.ChildNodes)
                        {
                            child.children.Add(GetChildJson(firstNode));
                        }
                    }
                    else
                    {
                        child.children.Add(new Child
                        {
                            type = "text",
                            value = htmlNode.InnerHtml
                        });
                    }
                    break;
                default:
                    child.type = "text";
                    child.value = htmlNode.InnerHtml;
                    break;
            }

            return child;
        }

将JSON转Html

        public static string ShopifyJsonToHtml(string json)
        {
            string strHtml = string.Empty;
            RichTextResponse richTextResponse = JsonConvert.DeserializeObject<RichTextResponse>(json);
            foreach (var child in richTextResponse.children)
            {
                strHtml += GetChildHtml(child);
            }
            return strHtml;
        }
        public static string GetChildHtml(Child child)
        {
            string strHtml = string.Empty;
            switch (child.type)
            {
                case "heading":
                    strHtml += $"<h{child.level}>";
                    if (child.children.Any())
                    {
                        foreach (var firstChild in child.children)
                        {
                            strHtml += GetChildHtml(firstChild);
                        }
                    }
                    else
                    {
                        strHtml += GetHtmlText(child);
                    }
                    strHtml += $"</h{child.level}>";
                    break;
                case "paragraph":
                    strHtml += $"<p>";
                    if (child.children.Any())
                    {
                        foreach (var firstChild in child.children)
                        {
                            strHtml += GetChildHtml(firstChild);
                        }
                    }
                    else
                    {

                        strHtml += GetHtmlText(child);
                    }
                    strHtml += $"</p>";
                    break;
                case "link":
                    strHtml += $"<a";
                    if (!string.IsNullOrEmpty(child.url))
                    {
                        strHtml += $" href=\"{child.url}\"";
                    }
                    if (!string.IsNullOrEmpty(child.title))
                    {
                        strHtml += $" title=\"{child.title}\"";
                    }
                    if (!string.IsNullOrEmpty(child.target))
                    {
                        strHtml += $" target=\"{child.target}\"";
                    }
                    strHtml += ">";
                    if (child.children.Any())
                    {
                        foreach (var firstChild in child.children)
                        {
                            strHtml += GetChildHtml(firstChild);
                        }
                    }
                    else
                    {

                        strHtml += GetHtmlText(child);
                    }
                    strHtml += $"</a>";
                    break;
                case "list":
                    if (child.listType == "unordered")
                    {//无序列表
                        strHtml += "<ul>";
                        if (child.children.Any())
                        {
                            foreach (var firstChild in child.children)
                            {
                                strHtml += GetChildHtml(firstChild);
                            }
                        }
                        else
                        {

                            strHtml += GetHtmlText(child);
                        }
                        strHtml += "</ul>";
                    }
                    else if (child.listType == "ordered")
                    {//有序列表
                        strHtml += "<ol>";
                        if (child.children.Any())
                        {
                            foreach (var firstChild in child.children)
                            {
                                strHtml += GetChildHtml(firstChild);
                            }
                        }
                        else
                        {

                            strHtml += GetHtmlText(child);
                        }
                        strHtml += "</ol>";
                    }
                    break;
                case "list-item":
                    strHtml += "<li>";
                    if (child.children.Any())
                    {
                        foreach (var firstChild in child.children)
                        {
                            strHtml += GetChildHtml(firstChild);
                        }
                    }
                    else
                    {

                        strHtml += GetHtmlText(child);
                    }
                    strHtml += "</li>";
                    break;
                default:
                    strHtml += GetHtmlText(child);
                    break;
            }
            return strHtml;
        }
        public static string GetHtmlText(Child child)
        {
            string strHtml = $"{(child.bold ? "<strong>" : "")}{(child.italic ? "<em>" : "")}{child.value}{(child.bold ? "</strong>" : "")}{(child.italic ? "</em>" : "")}";
            return strHtml;
        }

实体类

        public class RichTextResponse
        {
            public string type { get; set; }
            public List<Child> children { get; set; }
        }
        public class Child
        {
            public string type { get; set; }
            public string value { get; set; }
            public bool bold { get; set; }
            public bool italic { get; set; }
            public string url { get; set; }
            public string title { get; set; }
            public string target { get; set; }
            public List<Child> children { get; set; }
            public int level { get; set; }
            public string listType { get; set; }
        }
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
富文本内容JSON解析错误可能是由于以下几个原因导致的: 1. JSON格式错误:富文本内容可能包含了JSON格式错误的部分,例如缺少引号、括号不匹配等。在解析JSON时,解析器无法正确识别这些错误的格式,导致解析错误。 2. 编码问题:富文本内容中可能包含了非法的编码字符,例如特殊字符、无效的Unicode字符等。解析器在解析JSON时无法识别这些非法字符,导致解析错误。 3. 数据结构错误:富文本内容在JSON中的数据结构可能与解析代码的预期不一致,导致解析错误。例如,解析代码期望某个字段是数组,但富文本内容中对应的字段却是字符串。 为了解决富文本内容JSON解析错误,可以采取以下方法: 1. 检查JSON格式:使用JSON验证工具,检查富文本内容中是否存在格式错误的部分,修复错误的格式。 2. 检查编码问题:检查富文本内容中的编码字符,确保其符合JSON规范。如果存在非法字符,可以尝试将其义或删除。 3. 检查数据结构:检查富文本内容中的数据结构,并与解析代码的预期进行对比。如果存在不一致的地方,可以调整解析代码或修改富文本内容的数据结构,使其一致。 总结起来,富文本内容JSON解析错误可能是由于JSON格式错误、编码问题或数据结构错误导致的。通过检查JSON格式、编码字符和数据结构,可以解决这些错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值