How to read a json file into build.gradle and use the values the strings in build.gradle file

痴心易碎 提交于 2020-01-01 04:50:42

问题


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"
     }
  }
}

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

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


回答1:


From Gradle you can execute any Groovy code and Groovy already has build-in JSON parsers.

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
    }
}


来源:https://stackoverflow.com/questions/39034992/how-to-read-a-json-file-into-build-gradle-and-use-the-values-the-strings-in-buil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!