I want to use the map using JavaScript.
I could get the expected output if I use static data.
var tempMap={91: "Live", 41: "Aggr-Pack", 92: "Test", 118: "Group", 115: "Inner Cartons", 122: "SNDC", 102: "Web Service", 82: "EXTERNAL_DEF", 137: "PARTIAL", 72: "Return", 112: "Kit Master Cartons", 46: "SHIP", 134: "VEHICLE_TYPE", 121: "SGTIN", 135: "VEHICLE_ID", 45: "DE-COM", 113: "Bulk Cartons ", 136: "VEHICLE_COLOR", 131: "CARRIER", 52: "Invoice Number", 101: "File", 114: "Product Master Cartons ", 81: "GLN", 42: "Kit", 53: "Shipping Number", 47: "RECEIVE", 111: "Pallet", 133: "SHIPMENT_TYPE", 71: "Sale", 43: "DIS", 116: "Kits", 21: "en", 123: "SSCC", 22: "es", 117: "Products", 44: "COM", 132: "EXPECTED_DELIVERY_DATE", 73: "Transfer", 51: "Purchase Order Number"};
var key=116;
alert(tempMap[key]);
I want to create the dynamic map from response.
The response is
"{91=Live, 41=Aggr-Pack, 92=Test, 118=Group, 115=Inner Cartons, 122=SNDC, 102=Web Service, 82=EXTERNAL_DEF, 137=PARTIAL, 72=Return, 112=Kit Master Cartons, 46=SHIP, 134=VEHICLE_TYPE, 121=SGTIN, 135=VEHICLE_ID, 45=DE-COM, 113=Bulk Cartons , 136=VEHICLE_COLOR, 131=CARRIER, 52=Invoice Number, 101=File, 114=Product Master Cartons , 81=GLN, 42=Kit, 53=Shipping Number, 47=RECEIVE, 111=Pallet, 133=SHIPMENT_TYPE, 71=Sale, 43=DIS, 116=Kits, 21=en, 123=SSCC, 22=es, 117=Products, 44=COM, 132=EXPECTED_DELIVERY_DATE, 73=Transfer, 51=Purchase Order Number}"
I modify the response as given in tempMap data using below javascript code.
var map=(str.replace(/=/gi,"\"= \"").replace(/,/gi,"\",\"").replace(/=/gi,":"));
map="{\""+map.substring(2,(map.length)-1)+"\"}"
But I could not get the output by passing key value. I don't want to use any other third party to do this.
-
Where do you get the response from? If you control the output, use a better data format, like JSON.Felix Kling– Felix Kling2012年05月07日 10:46:02 +00:00Commented May 7, 2012 at 10:46
3 Answers 3
Assuming that the RegEx to transform your response into JSON is correct (I did not test it), you have to pass it through a JSON parser before using it. All you have at before that is a mere String.
map= JSON.parse( "{\""+map.substring(2,(map.length)-1)+"\"}" );
Comments
You have almost got that right.
Adding whitespace characters to second replace
var map=(str.replace(/=/gi,"\"= \"").replace(/,\s*/gi,"\",\"").replace(/=/gi,":"));
map="{\""+map.substring(2,(map.length)-1)+"\"}";
So that later you could execute
map = JSON.parse(map);
or
map = eval("(" + map + ")");
if you are dealing with browsers that do not support JSON.
Comments
Since evaluating the source string into a javascript object is trivial, I assumed you're having problems to convert the response string from '{91=Live, 41=Aggr-Pack}' to '{"91": "Live", "41": "Aggr-Pack"}'.
// this pattern matches each attribution and saves references to its left
// and right sides; i.e: after match "41=Aggr-Pack", 2ドル is "41" and 3ドル is
// "Aggr-Pack".
var pat = /([\w -]+)=([\w -]+)/gi;
// this uses the referenced matches to build another string;
// i.e: '{91=Live, 41=Aggr-Pack}' -> '{"91": "Live", "41": "Aggr-Pack"}';
var tmp = str.replace(re, '"2ドル": "3ドル"');
// the following evaluates the string as a javascript object (you can also
// use eval(tmp):
var map = JSON.parse(tmp)
I hope it solves your problem.