拆分键值对响应(以“&"分隔)使用Javascript [英] Splitting a key value pair response seperated by a '&' using Javascript
问题描述
可能重复:
使用JavaScript解析查询字符串
Possible Duplicate:
Parse query string in JavaScript
我从服务器收到以下格式的响应-Status=105&Accno=1458874455&Name=XYZ&Bal=5,888.00
在这里&"表示分隔符.
I have a response that I receive from the server in this format - Status=105&Accno=1458874455&Name=XYZ&Bal=5,888.00
Here '&' signifies separator.
我需要遍历字符串响应,并基于&"在JavaScript中拆分此字符串并获取值并将其存储为:
I need to iterate through the string response and split this string in JavaScript based on the '&' and get the values and store them as :
Status=105
Accno=1458874455
Name=XYZ
Bal=5,888.00
以便我可以在相应的文本框中填充它们并下拉.
so that I can populate them in the respective textbox and drop down.
我正在使用jQuery和AJAX来接收响应.
I am using jQuery and AJAX for receiving the response.
推荐答案
您可以使用split来获取数据:
You can use split to get the data out:
data = response.split("&");
一旦您拥有键=值"格式的数据,就可以再次拆分并添加到字典中:
Once you have the data in the format "key=value", you can split again and add to dictionary:
processed_data = new Object();
for(i = 0; i < data.length, i++){
m = data[i].split("=");
processed_data[m[0]] = m[1];
}
您可以使用以下字典:
processed_data["Status"]
这篇关于拆分键值对响应(以“&"分隔)使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!