using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ryCommon;
using System;
using System.Data;

/// <summary>
/// json 的摘要说明
/// </summary>
public class Json
{
    string jsonText = "";
    public JObject jo;
    public Json(string _jsonText)
    {
        string xx = _jsonText.GetStr("descUrl", "counterApi",0,out int pos1,"");
        jsonText = _jsonText.Replace("+new Date,", "'',");
        jsonText = jsonText.Replace("!true,", "false,");
        if (xx != "") { jsonText = jsonText.Replace(xx, "          :'',\r\n        "); }
        try
        {
            if (jsonText == "")
            { jo = new JObject(); }
            else
                jo = (JObject)JsonConvert.DeserializeObject(jsonText);
        }
        catch(Exception)
        {
            jo = new JObject();
        }
    }
    public string Text
    {
        get { return jo.ToString(); }
        set { jo = (JObject)JsonConvert.DeserializeObject(jsonText); }
    }
    public string GetJsonValue(string name)
    {
        if (jo[name] == null) { return ""; }
        else
        { return jo[name].ToString(); }
    }
    public string GetJsonValue(string name,string defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        { return jo[name].ToString(); }
    }
    public bool GetJsonValue(string name,bool defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        {
            string value = jo[name].ToString().ToLower();
            return value == "1" || value == "true"; }
    }
    public int GetJsonValue(string name, int defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        { return jo[name].ToInt(defvalue); }
    }
    public Int64 GetJsonValue(string name, Int64 defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        { return jo[name].ToInt64(defvalue); }
    }
    public decimal GetJsonValue(string name, decimal defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        { return jo[name].ToDecimal(defvalue); }
    }
    public double GetJsonValue(string name, double defvalue)
    {
        if (jo[name] == null) { return defvalue; }
        else
        { return jo[name].ToDouble(defvalue); }
    }
    public DataTable GetJsonValueByTable(string name)
    {
        if (jo[name] == null) { return new DataTable(); }
        else
        {
            try
            {
                return JsonConvert.DeserializeObject(jo[name].ToString(), typeof(DataTable)) as DataTable;
            }
            catch { return new DataTable(); }
        }
    }
    public void Add(string Name, string value)
    {
        try
        {
            jo.Add(Name, value);
        }
        catch { }
    }
    public void Add(string Name, int value)
    {
        try
        {
            jo.Add(Name, value);
        }
        catch { }
    }
    public void Add(string Name, bool value)
    {
        try
        {
            jo.Add(Name, value?1:0);
        }
        catch { }
    }
    public void Add(string Name, decimal value)
    {
        try
        {
            jo.Add(Name, value);
        }
        catch { }
    }
    public void Add(string Name, DataTable value)
    {
        try
        {
            jo.Add(Name, Newtonsoft.Json.JsonConvert.SerializeObject(value));
        }
        catch { }
    }

}