如何用gradle任务解析.json文件并从中获取json数据? [英] How to parse .json file with a gradle task and get the json data from it?

查看:1909
本文介绍了如何用gradle任务解析.json文件并从中获取json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在gradle任务的帮助下解析xyz.json文件并获取其中的所有单独的json数据?例如。我想解析存储在我的资产文件夹中的xyz.json文件中的这些数据,并获取其中的所有值,例如。

  {
type:xyz,
属性:{
foo:{
type:pqr
},
bar:{
type:abc
},
baz:{
type:lmo
}
}
}

解决方案

  gradle myTask {
doLast {
def inputFile = new File(xyz.json)
def json = new JsonSlurper()。parseText (inputFile.text)
def labels = json.properties.foo.type //这将返回pqr
}
}


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

解决方案

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

这篇关于如何用gradle任务解析.json文件并从中获取json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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