您将如何在Android Studio中为此JSON编写代码?

时间:2018-11-17 06:11:23

标签: json

从下面提供的JSON中获取数据的代码是什么。这是我到目前为止所写的:

                JSONObject js = new JSONObject(response);
                    JSONArray jsonArray = js.getJSONArray(Constants.Article);



            } catch (Exception e) {

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
    );

    RequestQueue requestQueue = Volley.newRequestQueue(ctx);
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(stringRequest);

这是我的JSON。我对如何解析我的json数组“ Article”下方的json对象感到困惑

{ "Article":[ { "InvDepartmentId":"001000000000012", "InvDepartmentName":"mens", "InvCategoryId":"001000000000023", "InvCategoryName":"adult", "InvSubCategoryId":"001000000000021", "InvSubCategoryName":"abc", "ArticleId":"001000000000186", "ArticleNo":"test22246", "ArticleWSP":1100.00, "CreatedOn":"2018-09-14T12:51:04", "LastUpdate":"2018-09-14T12:51:30.823" }

1 个答案:

答案 0 :(得分:0)

我知道回答这个问题为时已晚。为了更好地理解它,始终保持开放学习总是更好的选择。 这里有一些技巧来解析Java中的json。谷歌发布了gson库,该库使解析json数据更加容易。 首先所有您的JSON数据不正确。我修改了JSON,并使用gson注释为JSON数据创建了POJO类。最后,我使用gson库解析了JSON。 将以下依赖项添加到您的gradle文件中。

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

修改JSON文件:

{
"Article":[
{
"InvDepartmentId":"001000000000012",
"InvDepartmentName":"mens",
"InvCategoryId":"001000000000023",
"InvCategoryName":"adult",
"InvSubCategoryId":"001000000000021",
"InvSubCategoryName":"abc",
"ArticleId":"001000000000186",
"ArticleNo":"test22246",
"ArticleWSP":1100.00,
"CreatedOn":"2018-09-14T12:51:04",
"LastUpdate":"2018-09-14T12:51:30.823"
}
]
}

我将所有POSO类都包含在同一个类文件中。您可以创建为单独的类文件。

import java.util.List;

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

public class JsonParse {
    public static String json = "{\r\n" + "\"Article\":[\r\n" + "{\r\n" + "\"InvDepartmentId\":\"001000000000012\",\r\n"
            + "\"InvDepartmentName\":\"mens\",\r\n" + "\"InvCategoryId\":\"001000000000023\",\r\n"
            + "\"InvCategoryName\":\"adult\",\r\n" + "\"InvSubCategoryId\":\"001000000000021\",\r\n"
            + "\"InvSubCategoryName\":\"abc\",\r\n" + "\"ArticleId\":\"001000000000186\",\r\n"
            + "\"ArticleNo\":\"test22246\",\r\n" + "\"ArticleWSP\":1100.00,\r\n"
            + "\"CreatedOn\":\"2018-09-14T12:51:04\",\r\n" + "\"LastUpdate\":\"2018-09-14T12:51:30.823\"\r\n" + "}\r\n"
            + "]\r\n" + "}";

    public static void main(String args[]) {
        Gson gson = new Gson();
        Example articleList = gson.fromJson(json, Example.class);
        System.out.println(articleList.getArticle().get(0).getInvCategoryName());
    }

    public class Article {

        @SerializedName("InvDepartmentId")
        @Expose
        private String invDepartmentId;
        @SerializedName("InvDepartmentName")
        @Expose
        private String invDepartmentName;
        @SerializedName("InvCategoryId")
        @Expose
        private String invCategoryId;
        @SerializedName("InvCategoryName")
        @Expose
        private String invCategoryName;
        @SerializedName("InvSubCategoryId")
        @Expose
        private String invSubCategoryId;
        @SerializedName("InvSubCategoryName")
        @Expose
        private String invSubCategoryName;
        @SerializedName("ArticleId")
        @Expose
        private String articleId;
        @SerializedName("ArticleNo")
        @Expose
        private String articleNo;
        @SerializedName("ArticleWSP")
        @Expose
        private Double articleWSP;
        @SerializedName("CreatedOn")
        @Expose
        private String createdOn;
        @SerializedName("LastUpdate")
        @Expose
        private String lastUpdate;

        public String getInvDepartmentId() {
            return invDepartmentId;
        }

        public void setInvDepartmentId(String invDepartmentId) {
            this.invDepartmentId = invDepartmentId;
        }

        public String getInvDepartmentName() {
            return invDepartmentName;
        }

        public void setInvDepartmentName(String invDepartmentName) {
            this.invDepartmentName = invDepartmentName;
        }

        public String getInvCategoryId() {
            return invCategoryId;
        }

        public void setInvCategoryId(String invCategoryId) {
            this.invCategoryId = invCategoryId;
        }

        public String getInvCategoryName() {
            return invCategoryName;
        }

        public void setInvCategoryName(String invCategoryName) {
            this.invCategoryName = invCategoryName;
        }

        public String getInvSubCategoryId() {
            return invSubCategoryId;
        }

        public void setInvSubCategoryId(String invSubCategoryId) {
            this.invSubCategoryId = invSubCategoryId;
        }

        public String getInvSubCategoryName() {
            return invSubCategoryName;
        }

        public void setInvSubCategoryName(String invSubCategoryName) {
            this.invSubCategoryName = invSubCategoryName;
        }

        public String getArticleId() {
            return articleId;
        }

        public void setArticleId(String articleId) {
            this.articleId = articleId;
        }

        public String getArticleNo() {
            return articleNo;
        }

        public void setArticleNo(String articleNo) {
            this.articleNo = articleNo;
        }

        public Double getArticleWSP() {
            return articleWSP;
        }

        public void setArticleWSP(Double articleWSP) {
            this.articleWSP = articleWSP;
        }

        public String getCreatedOn() {
            return createdOn;
        }

        public void setCreatedOn(String createdOn) {
            this.createdOn = createdOn;
        }

        public String getLastUpdate() {
            return lastUpdate;
        }

        public void setLastUpdate(String lastUpdate) {
            this.lastUpdate = lastUpdate;
        }

    }

    public class Example {

        @SerializedName("Article")
        @Expose
        private List<Article> article = null;

        public List<Article> getArticle() {
            return article;
        }

        public void setArticle(List<Article> article) {
            this.article = article;
        }

    }

}