Here is a long string (actually a JSON key value):
"\u003cspan title=\"5 gold badges\"\u003e\u003cspan class=\"badge1\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e5\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"8 silver badges\"\u003e\u003cspan class=\"badge2\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e8\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"57 bronze badges\"\u003e\u003cspan class=\"badge3\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e57\u003c/span\u003e\u003c/span\u003e"
I need a way to parse it to get the number of gold, silver and bronze badges, using simple JScript in Dashcode.
-
It looks nothing like JSON to me. It appears to be an escaped HTML fragment.Jim Blackler– Jim Blackler2011年04月07日 14:30:05 +00:00Commented Apr 7, 2011 at 14:30
-
So which part are you stuck on?dumbass– dumbass2024年12月03日 08:07:27 +00:00Commented Dec 3, 2024 at 8:07
4 Answers 4
function getBadgeCounts(s) {
var badgeCountRE = /title="(\d+)/g, match = null, counts = [];
while ((match = badgeCountRE.exec(s)) !== null) {
counts.push(match[1]);
}
return {gold: counts[0], silver: counts[1], bronze: counts[2]};
}
Without hardcoded medal names:
function getBadgeCounts(s) {
var badgeCountRE = /title="(\d+) (\w+)/g, match = null, counts = {};
while ((match = badgeCountRE.exec(s)) !== null) {
counts[match[2]] = match[1];
}
return counts;
}
Comments
This is what the string is:
'<span title="5 gold badges">
<span class="badge1">●</span>
<span class="badgecount">5</span>
</span>
<span title="8 silver badges">
<span class="badge2">●</span>
<span class="badgecount">8</span>
</span>
<span title="57 bronze badges">
<span class="badge3">●</span>
<span class="badgecount">57</span>
</span>'
Maybe you can add it to an unvisible div, so you can use DOM method to get the values you want
1 Comment
innerHTML= then run a jQuery on it.var str = "\u003cspan title=\"5 gold badges\"\u003e\u003cspan class=\"badge1\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e5\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"8 silver badges\"\u003e\u003cspan class=\"badge2\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e8\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"57 bronze badges\"\u003e\u003cspan class=\"badge3\"\u003e●\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e57\u003c/span\u003e\u003c/span\u003e";
var res = {
gold: 0,
silver: 0,
bronze: 0
};
/* using a RexExp
/ - delimeter
(\d+) - capturing one or more digits
\s+ - one or more whitespace characters
(gold|silver|bronze) - capturing the color
/g - delimeter (global flag)
to match the information in the title of the spans
and using the replace trick to populate res
*/
str.replace( /(\d+)\s+(gold|silver|bronze)/g, function( all, count, color ) {
res[color] += parseInt( count );
});
console.log( res ); // Object { gold=5, silver=8, bronze=57}
Comments
If you put that string into a jQuery call, you can then query the document fragment just like you would any other piece of HTML:
var badgeHTML = "..." // Your encoded string here
var parsedHTML = $(badgeHTML); // Returns a jQuery collection of HTML nodes