Reputation: 87
I doing a code with angular, and in one case the json returns one value like a payload and I need to decrypt that.
I have a payload that like that:
I need to decode that. Like the following way: For example the part $B the vaule I need only the value 14, for the $s the value -58,etc. And I don't know hoy I can do it. For the moment only I split for the $.
var payload = event.eventpayload;
var dec = payload.split("$");
Upvotes: 1
Views: 59
Reputation: 945
You could use a regular expression to retrieve the values and then loop through the matches to create a dictionary.
Sample in C#:
const string payload = "$ST$I0$O0$B14.00$M1$S-058$D0000.03$X_";
var re = new Regex(@"(?<key>\$[A-Z])(?<value>[^$]*)");
foreach (Match m in re.Matches(payload)) {
Console.WriteLine("{0}: {1}", m.Groups["key"].Value, m.Groups["value"].Value);
}
Output:
$S: T
$I: 0
$O: 0
$B: 14.00
$M: 1
$S: -058
$D: 0000.03
$X: _
Upvotes: 1
Reputation: 7197
If it is always only one char after the $
, you can do something like this
var eventpayload="$ST$I0$O0$B14.00$M1$S-58$D0000.03$X_"
var earr = eventpayload.split("$").splice(1);
obj={};
for (var i=0; i < earr.length; i++) {
obj[earr[i][0]] = earr[i].slice(1);
}
console.log(obj);
you can also parseFloat() all values like this
var eventpayload="$ST$I0$O0$B14.00$M1$S-58$D0000.03$X_"
var earr = eventpayload.split("$").splice(1);
obj={};
for (var i=0; i < earr.length; i++) {
obj[earr[i][0]] = parseFloat(earr[i].slice(1));
}
console.log(obj);
Upvotes: 1