using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
//using Microsoft.CSharp;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString Json(string _json, string _key)
{
JObject obj = (JObject)JsonConvert.DeserializeObject(_json);
return new SqlString(obj[_key].ToString());
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString JsonArray(string _json, string _key, int _s)
{
JObject obj = (JObject)JsonConvert.DeserializeObject(_json);
JArray arr = (JArray)obj[_key];
return new SqlString(arr[_s].ToString());
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlInt32 JsonArraySize(string _json, string _key)
{
JObject obj = (JObject)JsonConvert.DeserializeObject(_json);
JArray arr = (JArray)obj[_key];
return new SqlInt32(arr.Count);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString JsonType(string _json, string _key)
{
JObject obj = (JObject)JsonConvert.DeserializeObject(_json);
return new SqlString(obj[_key].Type.ToString());
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString JsonObjectToString(string _json, string _key)
{
JObject obj = (JObject)JsonConvert.DeserializeObject(_json);
return new SqlString(JsonConvert.SerializeObject(obj[_key]).ToString());
}
};
殊企襴 郁 豢 貊蟇
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
//using Microsoft.CSharp;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString GetValue(string _json, string _key)
{
string val = "";
_json = _json.Replace(" : ", ":").Replace(" :", ":").Replace(": ", ":");
int key_position = _json.IndexOf(_key);
int sep_position = _json.IndexOf(":", key_position + _key.Length) + 1;
int val_position = _json.IndexOf(",", sep_position + 1);
//襷讌襷企..
if (val_position == -1)
val_position = _json.IndexOf("}", sep_position + 1);
if (_json.Substring(sep_position, 1) == "{")
{
val_position = _json.IndexOf("}", sep_position) + 1;
val = _json.Substring(sep_position, val_position - sep_position);
}
else if (_json.Substring(sep_position, 1) == "[")
{
val_position = _json.IndexOf("]", sep_position) + 1;
val = _json.Substring(sep_position, val_position - sep_position);
}
else
val = _json.Substring(sep_position, val_position - sep_position);
val = val.Replace("\"", "");
return new SqlString(val);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString ArrayGetValue(string _json, string _key, int _s)
{
string[] val_arrary;
string val = "";
_json = _json.Replace(" : ", ":").Replace(" :", ":").Replace(": ", ":");
int key_position = _json.IndexOf(_key);
int sep_position = _json.IndexOf(":", key_position + _key.Length) + 1;
int val_position = _json.IndexOf(",", sep_position + 1);
//襷讌襷企..
if (val_position == -1)
val_position = _json.IndexOf("}", sep_position + 1);
val_position = _json.IndexOf("]", sep_position) + 1;
val = _json.Substring(sep_position, val_position - sep_position);
val_arrary = val.Replace("[", "").Replace("]", "").Replace("\"", "").Replace(" ", "").Split(',');
return new SqlString(val_arrary[_s]);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlInt32 ArraySize(string _json, string _key)
{
string[] val_arrary = { };
string val = "";
_json = _json.Replace(" : ", ":").Replace(" :", ":").Replace(": ", ":");
int key_position = _json.IndexOf(_key);
int sep_position = _json.IndexOf(":", key_position + _key.Length) + 1;
int val_position = _json.IndexOf(",", sep_position + 1);
//襷讌襷企..
if (val_position == -1) val_position = _json.IndexOf("}", sep_position + 1);
val_position = _json.IndexOf("]", sep_position + 1);
val = _json.Substring(sep_position, val_position - sep_position);
val_arrary = val.Replace("[", "").Replace("]", "").Replace("\"", "").Replace(" ", "").Split(',');
return new SqlInt32(val_arrary.Length);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString JsonType(string _json, string _key)
{
string valueType = "String";
_json = _json.Replace(" : ", ":").Replace(" :", ":").Replace(": ", ":");
int key_position = _json.IndexOf(_key);
int sep_position = _json.IndexOf(":", key_position + _key.Length) + 1;
int val_position = _json.IndexOf(",", sep_position + 1);
//襷讌襷企..
if (val_position == -1) val_position = _json.IndexOf("}", sep_position + 1);
//螳
string first_string = _json.Substring(sep_position, 1);
if (first_string == "[") valueType = "Array";
else if (first_string == "{") valueType = "Object";
return new SqlString(valueType);
}
};