我从我的网络服务器得到了这个回复。我如何在我的使用改装1.9.0的应用程序中使用这样的响应

时间:2018-01-22 11:13:16

标签: java android json retrofit

如何解析数组类型中的这种响应并在我的应用中使用(使用改造)

   ["amount",{"amount_id":"1","amount":"10","n":"100"},
   {"amount_id":"2","amount":"100","n":"10000"},
   {"amount_id":"3","amount":"500","n":"50000"},
   {"amount_id":"4","amount":"1000","n":"100000"},
   {"amount_id":"5","amount":"5000","n":"500000"}]

1 个答案:

答案 0 :(得分:-1)

使用Gson

解析JSON响应的简便方法

Here创建如下的模型类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class AmountsModel {

        @SerializedName("amount_id")
        @Expose
        private String amountId;
        @SerializedName("amount")
        @Expose
        private String amount;
        @SerializedName("n")
        @Expose
        private String n;

        public String getAmountId() {
            return amountId;
        }

        public void setAmountId(String amountId) {
            this.amountId = amountId;
        }

        public String getAmount() {
            return amount;
        }

        public void setAmount(String amount) {
            this.amount = amount;
        }

        public String getN() {
            return n;
        }

        public void setN(String n) {
            this.n = n;
        }

    }

当您收到响应时,请执行此操作以解析 JSONArray 的每个 JSONObject

AmountsModel amountList = gson.fromJson(jsonObject.toString(), AmountsModel.class);