首页 > 代码库 > 键值对参数转换为Jquery json data参数

键值对参数转换为Jquery json data参数

键值对:var postData = http://www.mamicode.com/new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("a", "1"));
postData.Add(new KeyValuePair<string, string>("b", "2"));
postData.Add(new KeyValuePair<string, string>("c", "3"));

例如: var person = { jd_username: username, jd_password: password, jd_remeber: remeber };
等同于:‘ jd_username= ‘+ username+‘ &jd_password= ‘+password+‘ &jd_remeber= ‘+remeber
注意,如果参数有反斜杠则替换掉: var txtfeedback = $("#txtFeedback").val();
txtfeedback = txtfeedback.replace(/\n/g, " ")
*/
function jsonData(obj) {
    var jsonStr = "({";
    for (var o in obj) {
        jsonStr += "\"" + o + "\":";
        jsonStr += "\"" + obj[o] + "\"";
        jsonStr += ",";
    }
    jsonStr = jsonStr.substring(0, jsonStr.length - 1);
    jsonStr += "})";
    return eval(jsonStr); //转换json格式
}

//json
result = "{result:‘ok‘,msg:‘审核通过‘}";
result = eval("[" + result + "]")[0];
result. Result=ok
result. Msg=通过