json的{json和包装器

时间:2016-06-06 14:07:29

标签: java json gson jira

我正试图从Jira获得一些演员阵容。包装器的代码用于Gson.fromJson调用。我曾经使用类似json字符串的东西,它没有一个数组,里面有我需要的信息,而且工作正常,所以问题似乎与数组有关,但我不是百分百肯定:

import com.google.gson.annotations.SerializedName;

public class JiraRoleJsonWrapper {
    @SerializedName("self")
    private String self;
    @SerializedName("name")
    private String name;
    @SerializedName("id")
    private int id;
    @SerializedName("description")
    private String description;
    @SerializedName("actors")
    private JiraActors[] actors;

    public JiraActors[] getActors() {
        return actors;
    }
    public void setActors(JiraActors[] actors) {
        this.actors = actors;
    }

    public String getSelf() {
        return self;
    }

    public void setSelf(String self) {
        this.self = self;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String key) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /*
    public String[] getAvatarUrls() {
        return avatarUrls;
    }

    public void setAvatarUrls(String[] avatarUrls) {
        this.avatarUrls = avatarUrls;
    }
    */
}

class JiraActors {
    @SerializedName("id")
    private int id;
    @SerializedName("displayNme")
    private String displayName;
    @SerializedName("type")
    private String type;
    @SerializedName("name")
    private String name;
    //@SerializedName("avatarUrl")
    //private String avatarUrl;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

} 

它会收到的json:

{
    "self":"http://someserver.com:8080/apps/jira/rest/api/2/project/10741/role/10002",
"name":"Administrators",
"id":10002,
"description":"A project role",
"actors":[
    {
        "id":12432,
        "displayName":"Joe Smith",
        "type":"atlassian-user-role-actor",
        "name":"joesmi",
        "avatarUrl":"/apps/jira/secure/useravatar?size=xsmall&ownerId=dawsmi&avatarId=12245"
    },
    {
        "id":12612,
        "displayName":"Smurfette Desdemona",
        "type":"atlassian-user-role-actor",
        "name":"smudes",
        "avatarUrl":"/apps/jira/secure/useravatar?size=xsmall&ownerId=lamade&avatarId=10100"
    },

这显示了两个actor和json的格式。请注意我没有完整的json回复。它只展示了两个演员。

在我的代码中,我尝试了以下方法来检索actor:

InputStream is = response.getEntityInputStream();
                Reader reader = new InputStreamReader(is);
                Gson gson = new Gson();

                JiraRoleJsonWrapper[] jiraRoleJsonWrapper = gson.fromJson(reader, JiraRoleJsonWrapper[].class);
                for (JiraRoleJsonWrapper w : jiraRoleJsonWrapper) {
                    JiraActors[] a = w.getActors();
                    String name = a.getName();

由于某种原因,它找不到getName。我不知道为什么。

1 个答案:

答案 0 :(得分:1)

我明白了。

我将setActors更改为

import Utils from './utils'

export class MyClass {
     constructor()
     {
         Utils.doSomething("test");
     }
}

然后我能够获取数组列表并访问JiraActors的getName()方法。