### MyHouse V1.0.2502.0801 - *.[新增]适配新版接口。 ### SmartHouseAPI V1.0.2502.0801 - *.[新增]支持Docker部署,支持NAS。
241 lines
6.5 KiB
C#
241 lines
6.5 KiB
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using ryCommon;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyHouse.API
|
|
{
|
|
/// <summary>
|
|
/// json 的摘要说明
|
|
/// </summary>
|
|
public class Json
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public JObject jo;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Json()
|
|
{
|
|
jo = new JObject();
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="_jsonText"></param>
|
|
public Json(string _jsonText)
|
|
{
|
|
if (_jsonText.Length == 0)
|
|
{
|
|
jo = new JObject(); return;
|
|
}
|
|
string xx = _jsonText.GetStr("descUrl", "counterApi");
|
|
var jsonText = _jsonText.Replace("+new Date,", "'',");
|
|
jsonText = jsonText.Replace("!true,", "false,");
|
|
if (xx.Length > 0) { jsonText = jsonText.Replace(xx, " :'',\r\n "); }
|
|
try
|
|
{
|
|
if (jsonText.Length == 0)
|
|
{ jo = new JObject(); }
|
|
else
|
|
jo = (JObject)JsonConvert.DeserializeObject(jsonText);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
jo = new JObject();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取或设置json文本
|
|
/// </summary>
|
|
public string Text
|
|
{
|
|
get { return jo.ToString(); }
|
|
set
|
|
{
|
|
try
|
|
{
|
|
jo = (JObject)JsonConvert.DeserializeObject(value);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <returns></returns>
|
|
public string GetJsonValue(string name)
|
|
{
|
|
if (jo[name] == null) { return ""; }
|
|
else
|
|
{ return jo[name].ToString(); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
public string GetJsonValue(string name, string defvalue)
|
|
{
|
|
if (jo[name] == null) { return defvalue; }
|
|
else
|
|
{ return jo[name].ToString(); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
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";
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
public int GetJsonValue(string name, int defvalue)
|
|
{
|
|
if (jo[name] == null) { return defvalue; }
|
|
else
|
|
{ return jo[name].ToInt(defvalue); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
public Int64 GetJsonValue(string name, Int64 defvalue)
|
|
{
|
|
if (jo[name] == null) { return defvalue; }
|
|
else
|
|
{ return jo[name].ToInt64(defvalue); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
public decimal GetJsonValue(string name, decimal defvalue)
|
|
{
|
|
if (jo[name] == null) { return defvalue; }
|
|
else
|
|
{ return jo[name].ToDecimal(defvalue); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="defvalue"></param>
|
|
/// <returns></returns>
|
|
public double GetJsonValue(string name, double defvalue)
|
|
{
|
|
if (jo[name] == null) { return defvalue; }
|
|
else
|
|
{ return jo[name].ToDouble(defvalue); }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <returns></returns>
|
|
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(); }
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name"></param>
|
|
/// <param name="value"></param>
|
|
public void Add(string Name, string value)
|
|
{
|
|
try
|
|
{
|
|
jo.Add(Name, value);
|
|
}
|
|
catch { }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name"></param>
|
|
/// <param name="value"></param>
|
|
public void Add(string Name, int value)
|
|
{
|
|
try
|
|
{
|
|
jo.Add(Name, value);
|
|
}
|
|
catch { }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name"></param>
|
|
/// <param name="value"></param>
|
|
public void Add(string Name, bool value)
|
|
{
|
|
try
|
|
{
|
|
jo.Add(Name, value ? 1 : 0);
|
|
}
|
|
catch { }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name"></param>
|
|
/// <param name="value"></param>
|
|
public void Add(string Name, decimal value)
|
|
{
|
|
try
|
|
{
|
|
jo.Add(Name, value);
|
|
}
|
|
catch { }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name"></param>
|
|
/// <param name="value"></param>
|
|
public void Add(string Name, DataTable value)
|
|
{
|
|
try
|
|
{
|
|
jo.Add(Name, Newtonsoft.Json.JsonConvert.SerializeObject(value));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
}
|
|
}
|