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;
        }
    }
}
