【C#】【JSON】將JSON字串(巢狀、{開頭的、[開頭的...)解析(parse)為結構化的字串

以下程式碼,參數seprator
傳入'|',再將傳回字串replace("|","\r\n"),將'|'改成換行符號"\r\n"時,可用於視覺化呈現;
傳入&時,,則可用於將postdata轉為QueryString

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

//...(這裡有namespace、class...等等宣告.......)

        /// <summary>
        /// 將JSON字串解析為結構化的字串(seprator傳入換行符號時,可用於視覺化呈現;傳入&時,可用於將postdata轉為QueryString)
        /// </summary>
        /// <param name="dataObject"></param>
        /// <param name="seprator"></param>
        /// <returns></returns>
        public string JSON_to_PairString(object dataObject, char seprator)
        {
            string result = string.Empty;
            try
            {
                string sDataObject = JsonConvert.SerializeObject(dataObject);
                StringBuilder sb = new StringBuilder();

                object rsp = new object();
                JToken jtoken = JToken.Parse(sDataObject);
                if (jtoken is JArray)
                {
                    /*
                    //例如這樣的JSON結構,一開始就是JArray(中括號的數組[]):
                    "[{'ID':0,'List_Item_Type':'ApplyBu','CodeCategory_Name':null},
                    {'ID':1,'List_Item_Type':'ApplyBu','CodeCategory_Name':null},
                    {'ID':2,'List_Item_Type':'ApplyBu','CodeCategory_Name':null}]"
                    */
                    foreach (JObject jObject in ((JArray)jtoken).ToList())
                    {
                        foreach (var item in jObject)
                        {
                            sb.Append($"{item.Key}={item.Value}{seprator}");
                        }
                        sb.Append($"{seprator}");
                    }
                }
                else if (jtoken is JObject)
                {
                    /*
                    //例如這樣的JSON結構,一開始是JObject(大括號的數組{}),內部可能再包含JArray或JObject(巢狀):
                    "{ 'context_name': { 'lower_bound': 'value', 'upper_bound': 'value', 'values': [ 'value1', 'valueN' ] } }"
                    或"{'id':2,'name':'root','descendants':[{'id':4,'name':'node2','descendants':[{'id':7,'name':'node21'},{'id':8,'name':'node22'}]},{'id':3,'name':'node1','descendants':[{'id':5,'name':'node11'},{'id':6,'name':'node12'}]}]}"
                    */
                    foreach (var item in (JObject)jtoken)
                    {
                        sb.Append($"{item.Key}={item.Value}{seprator}");
                    }
                }

                result = sb.ToString();
            }
            catch (Exception err)
            {
                Console.WriteLine($"\r\n●Error={err.Message}");
            }

            result = result.TrimEnd(seprator);
            return result;
        }