Created
March 1, 2023 08:37
-
-
Save nakasyou/da7b38450af8666c563fccc22585d764 to your computer and use it in GitHub Desktop.
json-scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(Scratch) { | |
'use strict'; | |
class Json2Scratch { | |
getInfo () { | |
return { | |
id: 'parse', | |
name: 'Parse', | |
blocks: [ | |
{ | |
opcode: 'parse', | |
blockType: Scratch.BlockType.REPORTER, | |
text: 'JSON parse [JSON] of [PATH]', | |
arguments: { | |
JSON: { | |
type: Scratch.ArgumentType.STRING, | |
defaultValue: '{"a":["b","c"]}' | |
}, | |
PATH: { | |
type: Scratch.ArgumentType.STRING, | |
defaultValue: 'a.0' | |
} | |
} | |
} | |
] | |
}; | |
} | |
parse(args){ | |
try{ | |
const json=JSON.parse(args.JSON); | |
const path=args.PATH.split("."); | |
let value=json; | |
for(const locate of path){ | |
value=value[locate]; | |
} | |
return value; | |
}catch{return "NaN"} | |
} | |
} | |
Scratch.extensions.register(new Json2Scratch()); | |
})(Scratch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment