C# Net 通用 json 转 Object 对象
C# Net 提取 json 字符串 对象 数组
C# Net json 对象 中有字符串 转为 对象
例如输入:{"1":1,"a":"aa","aa":"{\"2\":2,\"bb\":\"{\\\"3\\\":3,\\\"cc\\\":\\\"ccc\\\"}\"}"}
例如输出:{"1":1,"a":"aa","aa":{"2":2,"bb":{"3":3,"cc":"ccc"}}}
-----------------------------------------------------------------------------------
----------------如需看实现的效果,请翻到文章最后-------------------
-----------------------------------------------------------------------------------
引用包【Json.NET】 加入 名称空间
using System; using Newtonsoft.Json.Linq;
创建一个【JsonHelper.cs】文件:
在【JsonHelper】类替换成如下代码:
public class JsonHelper { #region 提取json对象 ////// 提取json字符串对象(尽量不用此重载) /// 例如输入:{"1":1,"a":"aa","aa":"{\"2\":2,\"bb\":\"{\\\"3\\\":3,\\\"cc\\\":\\\"ccc\\\"}\"}"} /// 例如输出:{"1":1,"a":"aa","aa":{"2":2,"bb":{"3":3,"cc":"ccc"}}} /// public static JObject ExtractObj(string jsonObject) { return ExtractObj(JObject.Parse(jsonObject)); } ////// 提取json对象 /// 例如输入:{"1":1,"a":"aa","aa":"{\"2\":2,\"bb\":\"{\\\"3\\\":3,\\\"cc\\\":\\\"ccc\\\"}\"}"} /// 例如输出:{"1":1,"a":"aa","aa":{"2":2,"bb":{"3":3,"cc":"ccc"}}} /// public static JObject ExtractObj(JObject job) { //方法一:慢(3700个字符耗时2.2-2.4秒) //foreach (var item in job) //{ // try // { // string itemStr = item.Value.ToString(); // JObject itemJObj = JObject.Parse(itemStr); // JObject robj = ExtractObj(itemJObj); // job[item.Key] = robj; // } // catch // { // try // { // string itemStr = item.Value.ToString(); // JArray itemJArr = JArray.Parse(itemStr); // JArray rArr = ExtractArr(itemJArr); // job[item.Key] = rArr; // } // catch // { // } // } //} //return job; //方法二:快(3700个字符耗时40-60毫秒) foreach (var item in job) { var itemV = item.Value; if (itemV.Type == JTokenType.String) { var jtStr = itemV.ToString(); if (!IsJson(jtStr)) continue; JToken jToken = JToken.Parse(jtStr); if (jToken.Type == JTokenType.Object) { job[item.Key] = ExtractObj((JObject)jToken); } else if (jToken.Type == JTokenType.Array) { job[item.Key] = ExtractArr((JArray)jToken); } } else if (itemV.Type == JTokenType.Object) { job[item.Key] = ExtractObj((JObject)itemV); } else if (itemV.Type == JTokenType.Array) { job[item.Key] = ExtractArr((JArray)itemV); } } return job; } #endregion #region 提取json数组 ////// 提取json字符串数组(尽量不用此重载) /// 例如输入:["5","6","[\"3\",\"4\",\"[\\\"1\\\",\\\"2\\\"]\"]"] /// 例如输出:["5","6",["3","4",["1","2"]]] /// public static JArray ExtractArr(string jsonArr) { return ExtractArr(JArray.Parse(jsonArr)); } ////// 提取json数组 /// 例如输入:["5","6","[\"3\",\"4\",\"[\\\"1\\\",\\\"2\\\"]\"]"] /// 例如输出:["5","6",["3","4",["1","2"]]] /// /// ///public static JArray ExtractArr(JArray jArr) { //方法一:慢(3700个字符耗时2.2-2.4秒) //for (int i = 0; i < jArr.Count; i++) //{ // try // { // string itemStr = jArr[i].ToString(); // JArray itemJArr = JArray.Parse(itemStr); // JArray rArr = ExtractArr(itemJArr); // jArr[i] = rArr; // } // catch // { // try // { // string itemStr = jArr[i].ToString(); // JObject itemJObj = JObject.Parse(itemStr); // JObject robj = ExtractObj(itemJObj); // jArr[i] = robj; // } // catch // { // } // } //} //return jArr; //方法二:快(3700个字符耗时40-60毫秒) for (int i = 0; i < jArr.Count; i++) { JToken jToken = jArr[i]; if (jToken.Type == JTokenType.String) { var jtStr = jToken.ToString(); if (!IsJson(jtStr)) continue; JToken jToken2 = JToken.Parse(jtStr); if (jToken2.Type == JTokenType.Array) { jArr[i] = ExtractArr((JArray)jToken2); } else if (jToken2.Type == JTokenType.Object) { jArr[i] = ExtractObj((JObject)jToken2); } } else if (jToken.Type == JTokenType.Array) { jArr[i] = ExtractArr((JArray)jToken); } else if (jToken.Type == JTokenType.Object) { jArr[i] = ExtractObj((JObject)jToken); } } return jArr; } #endregion #region 提取json对象或数组 /// /// 提取json字符串(支持对象或数组)(尽量不用此重载) /// 例如输入:["5","6","[\"3\",\"4\",\"[\\\"1\\\",\\\"2\\\"]\"]","{\"1\":2,\"a\":\"ab\"}"] /// 例如输出:["5","6",["3","4",["1","2"]],{"1":2,"a":"ab"}] /// /// ///public static JToken ExtractAll(string json) { try { return ExtractAll(JToken.Parse(json)); } catch { throw new Exception("不是有效的JToken对象"); } } /// /// 提取json字符串(支持对象或数组) /// 例如输入:["5","6","[\"3\",\"4\",\"[\\\"1\\\",\\\"2\\\"]\"]","{\"1\":2,\"a\":\"ab\"}"] /// 例如输出:["5","6",["3","4",["1","2"]],{"1":2,"a":"ab"}] /// /// ///public static JToken ExtractAll(JToken jToken) { if (jToken.Type == JTokenType.String) { jToken = JToken.Parse(jToken.ToString()); } if (jToken.Type == JTokenType.Object) { return ExtractObj((JObject)jToken); } else if (jToken.Type == JTokenType.Array) { return ExtractArr((JArray)jToken); } else { throw new Exception("暂不支持提取[" + jToken.Type.ToString() + "]类型"); } } #endregion #region 验证 /// /// 是否为json(开头是{或[) /// public static bool IsJson(string json) { json = json.Trim(); if (string.IsNullOrEmpty(json)) return false; var t = json.First(); if (t == '{' || t == '[') return true; return false; } #endregion }
创建控制台【测试项目】:
加入包【Json.NET】,并加入如下名称空间:
using Newtonsoft.Json; using System.Collections.Generic;
在控制台中调用:
static void Main(string[] args) { Dictionarykeys = new Dictionary { { "1",2 }, { "a", "ab" }, //{ "ab", JsonConvert.SerializeObject(vs3) }, }; List vs = new List () { "1", "2" }; List vs2 = new List () { "3", "4", JsonConvert.SerializeObject(vs) }; List vs3 = new List () { "5", "6", JsonConvert.SerializeObject(vs2), JsonConvert.SerializeObject(keys) }; var ss = JsonHelper.ExtractArr(JsonConvert.SerializeObject(vs3)); //var ss = JsonHelper.ExtractObj(JsonConvert.SerializeObject(keys)); string sss = ss.ToString(); }
在【www.json.cn】下的可视化结果:
原json:
处理过后的json:
完成!