C#后台拼接复杂json,并返回前端。

废话不多说,直接上代码

C#code

     if (requestType == "请求的条件")
                {

                MySqlDataReader reader = MysqlHelper.ExecuteReader(" SELECT* FROM table where 条件='条件'");//数据库查询语句在这里

                    char[] specialChars = new char[] { ',' };//拼接json字符串开始
                    string JSONstring = "[";//这里是开始位置
                    try
                    {
                        while (reader.Read())
                        {
                            if (reader.HasRows)
                            {
                                JSONstring += "{";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";                                  
                                JSONstring += "\"" + "detail" + "\"";
                                JSONstring += ":";
                                JSONstring += "[";

                                MySqlDataReader reader = MysqlHelper.ExecuteReader(" SELECT* FROM table where 条件='条件'");//第二个查询写在这里,里面可以循环多个参数
                                try
                                {
                                    while (readerT.Read())
                                    {

                                        if (readerT.HasRows)
                                        {
                                            JSONstring += "{";
                                            JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                            JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";
                                            JSONstring += "\"" + "变量名" + "\":\"" + reader.GetString("变量") + "\",";   
                                            JSONstring += "},";
                                        }
                                    }
                                }
                                catch { readerT.Close(); }
                                finally { readerT.Close(); }
                                JSONstring = JSONstring.TrimEnd(specialChars);
                                JSONstring += "]},";
                                Log.Error(JSONstring, "请求JSONstring页面的JSONstring参数中的值");
                            }
                        }
                        JSONstring = JSONstring.TrimEnd(specialChars);
                        JSONstring += "]";
                    }
                    catch { reader.Close(); }
                    finally { reader.Close(); }                    
                    Response.Write(MysqlHelper.Encrypt(JSONstring));
                }

json格式如下图:

C#后台拼接复杂json,并返回前端。_第1张图片

你可能感兴趣的:(c#)