SuperDesign/Source/开发辅助工具/Manager/Json.cs

63 lines
1.7 KiB
C#
Raw Normal View History

2020-11-28 08:15:13 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace .Manager
{
public class Json
{
public static string ConvertJsonString(string str)
{
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
public static bool IsArray(string str)
{
try
{
JArray jar = JArray.Parse(str);
return true;
}
catch { return false; }
}
public static bool IsJson(string str)
{
try
{
var jo= (JObject)JsonConvert.DeserializeObject(str);
return true;
}
catch(Exception ex)
{ return false; }
//if (JsonSplit.IsJson(str))//传入的json串
//{
// return true;
//}
//return false;
}
}
}