如何从Java中的JSON字符串中获取数组?

时间:2017-12-13 09:05:57

标签: java json parsing

我有一个JSON字符串,如下所示,我从Web服务调用中获取。如何从构建键检索数组。

 {
  "actions" : [
    {

    },
    null,
    {

    }
  ],
  "description" : "<a href=\"ws/product/target/product-10.7-SNAPSHOT.zip\" style=\"font-size: 50px\">Download Branch Build</a>",
  "displayName" : "EPIC-AUTODEV-8413",
  "displayNameOrNull" : null,
  "name" : "EPIC-AUTODEV-8413",
  "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/",
  "buildable" : false,
  "builds" : [
    {
      "number" : 71,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/71/"
    },
    {
      "number" : 70,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/70/"
    },
    {
      "number" : 69,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/69/"
    },
    {
      "number" : 68,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/68/"
    },
    {
      "number" : 67,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/67/"
    },
    {
      "number" : 66,
      "url" : "http://localhost:80/jenkins/view/Branch-Builds/job/EPIC-AUTODEV-8413/66/"
    },

}

我尝试了tree=builds[*],但它给了我这样的输出

{"builds":[{},{},{},{},{},{},{},{},{},{},{}]}

1 个答案:

答案 0 :(得分:0)

试用javascript方法JSON.parse,下面是示例

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

上面会给你一个对象,而不是你想要的部分。

如果您希望将对象转换回json而不是尝试JSON.stringify()

var myJSON = JSON.stringify(obj);