搜索一圈没找到个好的答案,且全是复制粘贴,求人不如求己,亲测可用。
Ymal 官网
使用工具(官网推荐)
https://github.com/EsotericSoftware/yamlbeans
步骤
1、下载jar包
https://github.com/EsotericSoftware/yamlbeans/releases
找最新版,当前YamlBeans 1.15
赠送链接: https://pan.baidu.com/s/1ub_lKSGuWvF8qpQ8lGLjyQ 密码: pkr5
2、导入jar包
implementation files('libs/yamlbeans-1.15.jar')
3、写个yaml文件,放到了sdcard根目录
url: www.baidu.com port: 8090
4、对应Bean类
package com.bloguan.test; /** * Created by bloguan on 2022/3/8 */ public class Config { public String url; public Integer port; @Override public String toString() { return "Config{" + "url='" + url + '\'' + ", port=" + port + '}'; } }
5、读yaml
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); YamlReader reader = null; try { String path = "/storage/emulated/0/ym"; File f = new File(path); reader = new YamlReader(new FileReader(f)); Config contact = reader.read(Config.class); Log.e("Main", "config " + contact); } catch (FileNotFoundException | YamlException e) { e.printStackTrace(); } } }
打印
完美~~
至于yaml其它规则不再试了,解析器能跑通应该也都行
解析器详细说明
https://github.com/EsotericSoftware/yamlbeans/blob/master/CN_README.md
转载请注明出处
发表评论