2

安卓该如何显示 slatejs 格式的数据?

 3 years ago
source link: https://www.v2ex.com/t/796631
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
安卓该如何显示 slatejs 格式的数据?

V2EX  ›  Android

安卓该如何显示 slatejs 格式的数据?

  pigspy · 1 天前 · 549 次点击

服务器接口返回的数据是 slatejs 的数据格式,类似

[{
	"type": "paragraph",
	"children": [{
		"type": "emotion",
		"id": 51,
		"children": [{
			"text": ""
		}]
	}, {
		"text": "来去也就剩下那么些人了。"
	}]
}]

安卓 APP 应该怎么显示呢?不想用 webview 来处理

12 条回复    2021-08-19 20:55:42 +08:00

learningman

learningman   1 天前

这不 json 吗

pigspy

pigspy   1 天前

@learningman

slatejs 是一个 web 端的富文本框架,这个数据结构翻译成 html 就是 `<p><img src="./51">来去也就剩下那么些人了。<p>`,
麻烦一点的办法就是要手写一个解析器,把 slatejs 的数据格式先转换为 html,然后再用安卓的 HTML 工具类来转换为 TextView 可以显示的数据结构
我想问问有没有办法直接在安卓端显示这种数据

wobuhuicode

wobuhuicode   1 天前

这种最简单的方法就是直接用 webview 。使用 js 来解析 js 。最后导出回去给 java

pigspy

pigspy   1 天前

@wobuhuicode
webview 估计是最后的备选方案,因为这是个发帖回帖的界面,原来整个模块都是 kotlin 写的,如果要换成 webview 的话,工作量太大了

wobuhuicode

wobuhuicode   1 天前

@pigspy webview 只是用来解析,不要求它用来展示。解析完了数据返回 kotlin 层,销毁 webview

tanranran

tanranran   1 天前

@pigspy #4 webview 和 kotlin 是两回事吧,建议 webview,自己写解析器的话,要考虑的问题太多了

zhanlanhuizhang

zhanlanhuizhang   16 小时 9 分钟前

等的就是这个时候,天降大任,你实现一个轮子。

ZoteTheMighty

ZoteTheMighty   16 小时 9 分钟前

直接 new 一个 webview 解析,这个 WebView 不需要展示, 只需要在 WebView 的 onLoadResource (好像是这个)里去拿解析的东西就好了, 然后退出销毁该 WebView 即可。

pigspy

pigspy   15 小时 8 分钟前 via Android

@wobuhuicode
明白你的意思了,非常感谢

ch2

ch2   10 小时 49 分钟前

这种是富文本,你写个 converter 转成安卓原生富文本元素就行了

aristolochic

aristolochic   7 小时 21 分钟前

Slate 的官方序列化示例就是 switch 判断 children.type 对应输出 html 片段+递归;期间只用到了 Text 类作为判断是否为文本节点结束递归的依据。
所以其实对着它的 spec 很快就能写出来,也不必要用它的 Text 类判断是否为文本,文本节点特征很明显嘛。
如果 case 不多的话,为了这个需求就启动一个 WebView 是不是有些 Overkill 了

pigspy

pigspy   4 小时 53 分钟前

@aristolochic

嗯,我现在就是照着官网的例子自己写了一个反序列化函数来转换成 html

关于   ·   帮助文档   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   1274 人在线   最高记录 5497   ·  

创意工作者们的社区

World is powered by solitude

VERSION: 3.9.8.5 · 20ms · UTC 17:49 · PVG 01:49 · LAX 10:49 · JFK 13:49
♥ Do have faith in what you're doing.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK