Gson字符串轉換對象數組


 

public  class Input
{
    private String title;
    private int formId;
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getFormId() {
        return formId;
    }

    public void setFormId(int formId) {
        this.formId = formId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Input{" +
                "title='" + title + '\'' +
                ", formId=" + formId +
                ", content='" + content + '\'' +
                '}';
    }
}


String fromData ="[{\"title\":\"賺幣和賞金可在哪查看\",\"formId\":292,\"content\":\"我的\"}, {\"title\":\"賺幣有什么用?\",\"formId\":293,\"content\":\"可用於抽獎\"}, {\"title\":\"提現一般哪天處理?\",\"formId\":295,\"content\":\"每周一處理,周三到賬\"}, {\"title\":\"任務審核周期多長?\",\"formId\":296,\"content\":\"一般不超過7天\"}, {\"title\":\"如何邀請好友(多選)\",\"formId\":297,\"content\":\"使用邀請碼-掃描二維碼-分享給好友\"}] ";

Type type = new TypeToken<ArrayList<Input>>() {
}.getType();

Gson gson = new Gson();
ArrayList<Input> arrayList= gson.fromJson(fromData,type);

for(Input input :arrayList)
{
    System.out.println(input);
}

 

輸出結果


Input{title='賺幣和賞金可在哪查看', formId=292, content='我的'}
Input{title='賺幣有什么用?', formId=293, content='可用於抽獎'}
Input{title='提現一般哪天處理?', formId=295, content='每周一處理,周三到賬'}
Input{title='任務審核周期多長?', formId=296, content='一般不超過7天'}
Input{title='如何邀請好友(多選)', formId=297, content='使用邀請碼-掃描二維碼-分享給好友'}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM