zoukankan      html  css  js  c++  java
  • 获得省市 json 后台代码

    string connString = ConfigurationManager.ConnectionStrings["connStr"].ToString();
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();
    string sql = "select *from [dbo].[CrmAddress]";
    SqlDataAdapter da = new SqlDataAdapter(sql,conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];
    DataRow[] arraySheng = dt.Select("CityLevel=1").OrderBy(e => e["CityCode"]).ToArray();
    DataRow[] arrayShi = dt.Select("CityLevel=2").OrderBy(e => e["CityCode"]).ToArray();
    Hashtable htshengshi = new Hashtable();
    foreach (var itemsheng in arraySheng)
    {
    List<KeyValuePair<string, string>> shiList = new List<KeyValuePair<string, string>>();
    foreach (var itemshi in arrayShi)
    {
    if (itemsheng["CityCode"].ToString().Trim() == itemshi["ParentID"].ToString().Trim())
    {
    shiList.Add(new KeyValuePair<string, string>(itemshi["CityCode"].ToString(), itemshi["CityName"].ToString()));
    }
    }
    htshengshi.Add(itemsheng["CityCode"].ToString(), shiList);
    }
    DataTable dtsheng = new DataTable();
    foreach (DataColumn col in dt.Columns)
    {
    dtsheng.Columns.Add(col.ColumnName);
    }
    foreach (var sheng in arraySheng)
    {
    dtsheng.Rows.Add(sheng.ItemArray);
    }
    StringBuilder jsonResult = new StringBuilder();
    jsonResult.Append("{");
    jsonResult.Append(""result":"1",");
    jsonResult.Append(""data":{");
    jsonResult.Append(""province":");
    jsonResult.Append(ListToJsonString(dtsheng));
    jsonResult.Append(",");
    jsonResult.Append(""city":");
    jsonResult.Append("[");
    string fengefu = "";
    foreach (DictionaryEntry itemSheng in htshengshi)
    {
    jsonResult.Append(fengefu+"{"");
    jsonResult.Append(itemSheng.Key.ToString());
    jsonResult.Append("":[");
    List<KeyValuePair<string,string>> tempCityList= itemSheng.Value as List<KeyValuePair<string,string>>;
    string fengefu2 = "";
    foreach (KeyValuePair<string,string> itemShi in tempCityList)
    {
    jsonResult.Append(fengefu2+"{");
    jsonResult.Append(""");
    jsonResult.Append(itemShi.Key.ToString());
    jsonResult.Append("":"");
    jsonResult.Append(itemShi.Value.ToString());
    jsonResult.Append(""}");
    fengefu2 = ",";
    }
    jsonResult.Append("]");
    jsonResult.Append("}");
    fengefu = ",";
    }
    jsonResult.Append("]}}");
    context.Response.ContentType = "text/plain";
    context.Response.Write(jsonResult);
    }
    /// <summary>
    /// 将DataSet中数据,json化
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public string ListToJsonString(DataTable source)
    {
    StringBuilder result = new StringBuilder();
    string fengefu0 = "";
    foreach (DataRow row in source.Rows)
    {
    result.Append(fengefu0 + "{");
    fengefu0 = ",";
    string fengefu = "";
    foreach (DataColumn col in source.Columns)
    {
    result.Append(fengefu + """ + col.ColumnName + "":" + """ + row[col].ToString().Trim() + """);
    fengefu = ",";
    }
    result.Append("}");
    }

    result.Insert(0, "[");
    result.Append("]");

    return result.ToString();
    }

  • 相关阅读:
    利用树莓派把普通打印机变成网络打印机方法
    Python之datetime库
    CentOS7从默认/home中转移空间到根分区/
    更改Azure虚拟机账号密码
    创建一个托管磁盘的Windows定制镜像
    IO多路复用详解
    玩转redis
    EF Linq to Sql 多表left join查询并对结果group by分组之后进行count,max等处理
    免费,主流的在线办公/协作,会议,文档,调查,分享工具推荐(持续维护中)
    多sql查询count合并为一行
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/5891985.html
Copyright © 2011-2022 走看看