如果把一个对象 转换成 字符串的形式?用于ajax的提交数据.{id:"x",name:"xx"}转换成"{id:\"x\",name:\"xx\"}"

解决方案 »

  1.   

    $.ajax({
                    type: "post",
                    url: "Handler.ashx",
                    data: { m: 'a' },
                    success: function(arr) {
                        var length = eval(arr).length;
                        var select1Str = [];
                        for (var i = 0; i < length; ++i) {
                            select1Str.push("<option value='", eval(arr)[i].name, "'>", eval(arr)[i].name, "</option>");
                        }
                        select1Str = select1Str.join("");
                        $("#select1").append(select1Str);                    //绑定事件change事件
                        $("#select1").change(function() {
                            alert($("#select1 option[selected]").val());
                        });
                    }
                });public void ProcessRequest (HttpContext context) 
        {
            char method = Convert.ToChar(context.Request.Params["m"]);
            context.Response.ContentType = "text/plain";        string arraryA = "[{name:\"柳州\"},{name:\"南宁\"},{name:\"桂林\"}]";
            string arraryB = "[{name:\"广西\"},{name:\"上海\"},{name:\"北京\"}]";
            string arraryC = "[{name:\"中国\"},{name:\"美国\"},{name:\"英国\"}]";
            string arraryD = "[{name:\"地球\"},{name:\"月球\"},{name:\"太阳\"}]";
            
            switch (method)
            {
                case 'a':
                    context.Response.Write(arraryA);
                    break;
                case 'b':
                    context.Response.Write(arraryB);
                    break;
                case 'c':
                    context.Response.Write(arraryC);
                    break;
                case 'd':
                    context.Response.Write(arraryD);
                    break;
            }
        }
      

  2.   

    http://www.javaeye.com/topic/121319也可遍历json对象,然后生成json字符串