简体   繁体   English

过滤 JSON API 结果最少

[英]Filtering JSON API results minimally

Im attemtpting to filter results from an API query and was wondering what the best case scenario would be to accomplish this goal.我正在尝试过滤来自 API 查询的结果,并且想知道实现此目标的最佳情况是什么。

https://public-api.solscan.io/account/METAmTMXwdb8gYzyCPfXXFmZZw4rUsXX58PNsDg7zjL https://public-api.solscan.io/account/METAmTMXwdb8gYzyCPfXXFmZZw4rUsXX58PNsDg7zjL

Results in:结果是:

{"lamports":291171461600,"ownerProgram":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","type":"token_account","rentEpoch":283,"account":"METAmTMXwdb8gYzyCPfXXFmZZw4rUsXX58PNsDg7zjL","tokenInfo":{"name":"Solice","symbol":"SLC","price":1.28,"volume":1253400,"decimals":6,"tokenAuthority":null,"supply":"400000000000000","type":"token_address"}}

However the intended goal is to only have the value of "supply" show, which in this case would be 400000000000000 .然而,预期的目标是只显示“供应”的价值,在本例中为400000000000000

Would need to be as minimal of a solution as possible while being able to be machine read.在能够被机器读取的同时需要尽可能少的解决方案。

My first thought was filter it with JS but that is reliant on processing the JS.我的第一个想法是用 JS 过滤它,但这依赖于处理 JS。

Aside from parsing the JSON into an object, and then extracting the field, you can use a regex and speed it up a bit.除了将 JSON 解析为 object,然后提取字段之外,您还可以使用正则表达式并加快速度。

const rx = /"supply":"([^"]*)"/
const m = rx.exec(unparsedJson)
const result = m ? m[1] : undefined

https://regex101.com/r/Vx3Cp6/1 https://regex101.com/r/Vx3Cp6/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM