循环遍历经典asp中的json响应

时间:2013-10-07 15:07:22

标签: json rest vbscript asp-classic

所以我正在向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中提取我需要的信息?

1 个答案:

答案 0 :(得分:1)

您检查this question relating to using the Gson library。它非常小巧,快速且易于使用,可以在JSON和Objects之间进行转换。

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

请注意,与每行结尾处的地址}相比,没有额外的{{1}}。