62,074
社区成员
发帖
与我相关
我的任务
分享
下一个 Newtonsoft.Json.dll 然后引用
调用
DataSet ds = new DataSet();
string tempstr = ToJson(ds);
JObject result = new JObject();
return result.ToString().Replace("\r\n", "");
/// <summary>
/// 对象转换为Json字符串
/// </summary>
/// <param name="jsonObject">对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(object jsonObject)
{
try
{
StringBuilder jsonString = new StringBuilder();
jsonString.Append("{");
PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
if (objectValue == null)
{
continue;
}
StringBuilder value = new StringBuilder();
if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
{
value.Append("\"" + objectValue.ToString() + "\"");
}
else if (objectValue is string)
{
value.Append("\"" + objectValue.ToString() + "\"");
}
else if (objectValue is IEnumerable)
{
value.Append(ToJson((IEnumerable)objectValue));
}
else
{
value.Append("\"" + objectValue.ToString() + "\"");
}
jsonString.Append("\"" + propertyInfo[i].Name + "\":" + value + ","); ;
}
return jsonString.ToString().TrimEnd(',') + "}";
}
catch (Exception ex)
{
throw ex;
}
}
<?php
/**
* author: nbsp - http://www.xujiantao.com
* 2012-09-22
*/
header("Content-type:text/html; charset=utf-8");
//$arr数组也可从数据库取出来的数据,为了效率不推荐使用数据库查询
$arr = array(
"Ädams, Egbert",
"Altman, Alisha",
"Archibald, Janna",
"Auman, Cody",
"Bagley, Sheree",
"Ballou, Wilmot",
"Bard, Cassian",
"Bash, Latanya",
"Beail, May",
"Black, Lux",
"Bloise, India",
"Blyant, Nora",
"Bollinger, Carter",
"Burns, Jaycob",
"Carden, Preston",
"Carter, Merrilyn",
"Christner, Addie",
"Churchill, Mirabelle",
"Conkle, Erin",
"Countryman, Abner",
"Courtney, Edgar",
"Cowher, Antony",
"Craig, Charlie",
"Cram, Zacharias",
"Cressman, Ted",
"Crissman, Annie",
"Davis, Palmer",
"Downing, Casimir",
"Earl, Missie",
"Eckert, Janele",
"Eisenman, Briar",
"Fitzgerald, Love",
"Fleming, Sidney",
"Fuchs, Bridger",
"Fulton, Rosalynne",
"Fye, Webster",
"Geyer, Rylan",
"Greene, Charis",
"Greif, Jem",
"Guest, Sarahjeanne",
"Harper, Phyllida",
"Hildyard, Erskine",
"Hoenshell, Eulalia",
"Isaman, Lalo",
"James, Diamond",
"Jenkins, Merrill",
"Jube, Bennett",
"Kava, Marianne",
"Kern, Linda",
"Klockman, Jenifer",
"Lacon, Quincy",
"Laurenzi, Leland",
"Leichter, Jeane",
"Leslie, Kerrie",
"Lester, Noah",
"Llora, Roxana",
"Lombardi, Polly",
"Lowstetter, Louisa",
"Mays, Emery",
"Mccullough, Bernadine",
"Mckinnon, Kristie",
"Meyers, Hector",
"Monahan, Penelope",
"Mull, Kaelea",
"Newbiggin, Osmond",
"Nickolson, Alfreda",
"Pawle, Jacki",
"Paynter, Nerissa",
"Pinney, Wilkie",
"Pratt, Ricky",
"Putnam, Stephanie",
"Ream, Terrence",
"Rumbaugh, Noelle",
"Ryals, Titania",
"Saylor, Lenora",
"Schofield, Denice",
"Schuck, John",
"Scott, Clover",
"Smith, Estella",
"Smothers, Matthew",
"Stainforth, Maurene",
"Stephenson, Phillipa",
"Stewart, Hyram",
"Stough, Gussie",
"Strickland, Temple",
"Sullivan, Gertie",
"Swink, Stefanie",
"Tavoularis, Terance",
"Taylor, Kizzy",
"Thigpen, Alwyn",
"Treeby, Jim",
"Trevithick, Jayme",
"Waldron, Ashley",
"Wheeler, Bysshe",
"Whishaw, Dodie",
"Whitehead, Jericho",
"Wilks, Debby",
"Wire, Tallulah",
"Woodworth, Alexandria",
"Zaun, Jillie",
"徐建涛",
"徐先生",
"徐女士",
"徐静蕾",
"徐怀钰",
"徐熙媛",
"张三",
"张栋梁",
"张可可",
"张杰",
"张柏芝",
"李四"
);
$input = strtolower($_GET["input"]);
$len = strlen($input);
//判断是中文还是英文
$language = preg_match("/^[".chr(0x80)."-".chr(0xff)."]+$/",$input);
$result = array();
$count = 0;
//如果为英文则截取处理
if(isset($len) && ($language == 0)){
for($i=0;$i<count($arr);$i++){
if(strtolower(substr($arr[$i],0,$len)) == strtolower($input)){
$count++;
$result[] = array("id"=>$i,"value"=>$arr[$i]);
}
}
}
//如果为中文则截取处理
if(isset($len) && ($language == 1)){
function msubstr($str, $start=0, $length, $charset="utf-8"){
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
return $slice;
}
//字符串长度
function abslength($str){
if(empty($str)){
return 0;
}
if(function_exists('mb_strlen')){
return mb_strlen($str,'utf-8');
}else{
preg_match_all("/./u", $str, $ar);
return count($ar[0]);
}
}
$cnlen = abslength($input);
//如果查找到则加入数组
for($i=0;$i<count($arr);$i++){
if(msubstr($arr[$i],0,$cnlen) == $input){
$count++;
$result[] = array("id"=>$i,"value"=>$arr[$i]);
}
}
}
//输出json数据
if(empty($input)){
echo "";
}else{
echo json_encode($result);
}
?>
我想用数据库读出来的结果,用C#来代替这段php代码 求大神,歇了,脑袋快爆炸了!
#region DataSet转换为Json
/// <summary>
/// DataSet转换为Json
/// </summary>
/// <param name="dataSet">DataSet对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(DataSet dataSet)
{
string jsonString = "{";
foreach (DataTable table in dataSet.Tables)
{
jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ",";
}
jsonString = jsonString.TrimEnd(',');
return jsonString + "}";
}
#endregion
#region Datatable转换为Json
/// <summary>
/// Datatable转换为Json
/// </summary>
/// <param name="table">Datatable对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(DataTable dt)
{
StringBuilder jsonString = new StringBuilder();
jsonString.Append("[");
DataRowCollection drc = dt.Rows;
for (int i = 0; i < drc.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
string strKey = dt.Columns[j].ColumnName;
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (j < dt.Columns.Count - 1)
{
jsonString.Append(strValue + ",");
}
else
{
jsonString.Append(strValue);
}
}
jsonString.Append("},");
}
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
}