1

所以我正在向 JIRA API 发出一个休息请求,并得到一个包含所有对象的 json 响应。

我的请求如下所示:

Set restReq = CreateObject("MSXML2.ServerXMLHTTP.3.0")
restReq.open "GET", "URI",False
restReq.setRequestHeader  "Authorization","Basic{user:Password}"
restReq.setOption SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS,SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
restReq.send("")
'response.write(restReq.responseText)   

response.write 看起来像这样(但更长):

[{"self":"https://JIRA:8343/rest/api/2/project/CT","id":"10004","key":"CT","name":"Core Technologies"}},
{"self":"https://JIRA:8343/rest/api/2/project/CTCCG","id":"10006","key":"CTCCG","name":"CT CCG"}}]

我希望能够遍历响应并在无序列表中使用“id”、“key”和“name”。我可以创建一个 ul,但是如何从 json 中提取我需要的信息?

4

1 回答 1

1

你检查这个与使用 Gson 库有关的问题。在 JSON 到对象之间转换非常小、快速且易于使用。

import java.io.FileReader;

import com.google.gson.Gson;

public class Test {

public static void main(String[] args) throws Exception
{
    Gson gson = new Gson();
    TypeDTO[] myTypes = gson.fromJson(new FileReader("D:\\temp\\inputjson.txt"), TypeDTO[].class);
    for (int i = 0; i < myTypes.length; ++i)
        System.out.println(myTypes[i].self);
 }

class TypeDTO
{
  String self;
  String id;
  String key;
  String name;
}
}

inputjson.txt 有

[{"self":"https://JIRA:8343/rest/api/2/project/CT","id":"10004","key":"CT","name":"Core Technologies"},
 {"self":"https://JIRA:8343/rest/api/2/project/CTCCG","id":"10006","key":"CTCCG","name":"CT CCG"}]

}请注意在每行末尾与您的相比没有附加。

于 2013-10-07T15:25:39.003 回答