• 获得省市 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();
    }

  • 相关阅读:
    javac不是内部或外部命令在win10上的解决方案
    HDU 6191 Query on A Tree(字典树+离线)
    hihoCoder #1558 : H国的身份证号码I
    HDU 6154 CaoHaha's staff(2017中国大学生程序设计竞赛
    湖南省第十二届省赛:Parenthesis
    POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
    HDU 2923 Relocation(状压dp+01背包)
    HDU 2546 饭卡(01背包)
    HDU 1247 Hat’s Words(字典树)
    HDU 1711 Number Sequence(KMP)附带KMP的详解
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/5891985.html
Copyright © 2020-2023  润新知