javascript - string in dictionary to dictionary -


i having string in form of dictionary in following way:var str1= '{ "t1" : "1"},{ "t1" : "2"}, { "t2" : "3"},{ "t2" : "4"}, { "t2" : "5"}, { "t3" : "6"}, { "t3" : "7"},{ "t3" : "8"},{ "t2" : "9"}, { "t2" : "10"}, { "t1" : "11"}, { "t1" : "12"}';

plz give idea how convert such behave tree structure. i.e. t1 search till next t1 in loop , append t1 , value in current iterated term , similar t2 , on.. example str2.

str2='{ "t1" : "1"} ,{ "t1" : "2", "t2" : "3"},{ "t1" :"2", "t2" : "4"}, { "t1" : "2", "t2" : "5", "t3" : "6"}, { "t1" : "2", "t2" : "5", "t3" : "7"},{ "t1" : "2", "t2" : "5", "t3" : "8"},  { "t1" : "2", "t2" : "9"},{ "t1" : "2", "t2" : "10"}, { "t1" : "11"}, { "t1" : "12"}'; 

i'm not sure why have structure this. it's not valid json, can't parse is, because can't have same property twice. so, need it's "normalize" bit, or values out of in way. example:

var str = '{ "t1" : "1"},{ "t1" : "2"}, { "t2" : "3"},{ "t2" : "4"}, { "t2" : "5"}, { "t3" : "6"}, { "t3" : "7"},{ "t3" : "8"},{ "t2" : "9"}, { "t2" : "10"}, { "t1" : "11"}, { "t1" : "12"}';  var re = /"([^"]*)"\s*:\s*"([^"]*)"/g; var dict = {};  (var matches; matches = re.exec(str);){     var key = matches[1];   (dict[key] || (dict[key] = [])).push(matches[2]); } 

that creates dict object like:

{t1:["1", "2", "11", "12"], t2:["3", "4", "5", "9", "10"], t3:["6", "7", "8"]} 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -