How to parse .json file with a gradle task and get the json data from it?

旧街凉风 提交于 2019-12-22 05:45:09

问题


Is there a way where in I can parse a xyz.json file with help of a gradle task and get all the individual json data inside it? for eg. I want to parse this data which is stored in a xyz.json file in my assets folder and get all the values inside it, eg. get the value of "type".

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

回答1:


You can create a gradle task like this

gradle myTask{
 doLast{
  def inputFile = new File("xyz.json")
  def json = new JsonSlurper().parseText(inputFile.text)
  def labels = json.properties.foo.type //This will return "pqr"
 }
}



回答2:


Gradle build scripts are just Groovy scripts. Read the Gradle User Guide to learn how to write custom tasks, and use the JsonSlurper class to parse your json file.



来源:https://stackoverflow.com/questions/37824883/how-to-parse-json-file-with-a-gradle-task-and-get-the-json-data-from-it

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