1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- // See https://aka.ms/new-console-template for more information
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using OrBit.MESInterface.OrderVerifyWebService.Application;
- string aaa = "{\r\n \"Code\": 200,\r\n \"Data\": [\r\n {\r\n \"ID\": \"1002311180000366\",\r\n \"DocNo\": \"HBRC231100035\"\r\n }\r\n ],\r\n \"Msg\": \"\"\r\n}";
- AnalyticResult analyticResult = new AnalyticResult();
- var jobj = DeserializeStringToObject(aaa);
- analyticResult.data = jobj;
- var jstr = JsonConvert.SerializeObject(analyticResult);
- Console.WriteLine(jstr);
- /// <summary>
- /// 将json字符串反序列化为字典类型
- /// </summary>
- /// <typeparam name="TKey">字典key</typeparam>
- /// <typeparam name="TValue">字典value</typeparam>
- /// <param name="jsonStr">json字符串</param>
- /// <returns>字典数据</returns>
- static Dictionary<string, object> DeserializeStringToDictionary(string jsonStr)
- {
- if (string.IsNullOrEmpty(jsonStr))
- return new Dictionary<string, object>();
- Dictionary<string, object> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
- return jsonDict;
- }
- /// <summary>
- /// 根据json字符串类型将json字符串反序列化为字典或者集合类型
- /// </summary>
- /// <param name="jsonStr"></param>
- /// <returns></returns>
- static object DeserializeStringToObject(string jsonStr)
- {
- if (isJArray(jsonStr))
- {
- return JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonStr);
- }
- return DeserializeStringToDictionary(jsonStr);
- }
- static bool isJArray(string json)
- {
- if (string.IsNullOrEmpty(json))
- {
- return false;
- }
- try
- {
- JArray.Parse(json);
- return true;
- }
- catch
- {
- return false;
- }
- }
|