如何将json文件读入build.gradle并使用build.gradle文件中的字符串值 [英] How to read a json file into build.gradle and use the values the strings in build.gradle file

查看:603
本文介绍了如何将json文件读入build.gradle并使用build.gradle文件中的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,
读取 build.gradle 中的json文件,并将json值用作文件中的字符串

for example, read the json file in build.gradle and use the json values as strings in the file

{
  "type":"xyz",
  "properties": {
    "foo": {
      "type": "pqr"
     },
     "bar": {
       "type": "abc"
     },
     "baz": {
       "type": "lmo"
     }
  }
}

我需要调用 properties.bar.type ,并且应该在其中替换 abc

I need to call properties.bar.type and abc should be replaced there.

我需要将这些值转换为 string 并在 build.gradle 中使用文件

I need to convert these values to string and use in build.gradle file

推荐答案

在Gradle中,您可以执行任何Groovy代码,并且Groovy已经具有内置的JSON解析器。

例如您可以使用一个可以将您的值打印到stdout中的任务:

E.g. you can use a task that will print your value into stdout:

task parseJson {
    doLast {
        def jsonFile = file('path/to/json')
        def parsedJson = new groovy.json.JsonSlurper().parseText(jsonFile.text)

        println parsedJson.properties.bar.type
    }
}

这篇关于如何将json文件读入build.gradle并使用build.gradle文件中的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆