简体   繁体   中英

Converting a json array to json object in android using gson?

I am passing a json array from activity A to activity B.Then I am using the GSON library to insert a value into the array.This is my current code.

public void gsonResponse(String json) {
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        for (int i = 0; i < jsonArray.length(); i++) {
            LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            JSONObject innerJosonObject = new JSONObject(jsonArray.getString(i));

            // you need to put all values from jsonObject to map for managing the order..

            linkedHashMap.put("doc_no", textViewInvNo.getText().toString());
            linkedHashMap.put("itembarcode", innerJosonObject.getString("itembarcode"));
            linkedHashMap.put("net_wt", innerJosonObject.getString("net_wt"));
            linkedHashMap.put("gross_wt", innerJosonObject.getString("gross_wt"));
            linkedHashMap.put("stone_wt", innerJosonObject.getString("stone_wt"));
            linkedHashMap.put("stone_amt", innerJosonObject.getString("stone_amt"));
            linkedHashMap.put("rate", innerJosonObject.getString("rate"));
            linkedHashMap.put("making", innerJosonObject.getString("making"));
            linkedHashMap.put("qty", innerJosonObject.getString("qty"));
            linkedHashMap.put("net_rate", innerJosonObject.getString("net_rate"));
            linkedHashMap.put("item_total", innerJosonObject.getString("item_total"));
            linkedHashMap.put("sum_total", innerJosonObject.getString("sum_total"));
            Gson gson = new Gson();
            // convert linkedHashMap to json string and it will keep the insertion order..
            String string = gson.toJson(linkedHashMap, LinkedHashMap.class);
            jsonArray.put(i, string);
        }
        jsonObject.put("result", jsonArray);
        String jsonResp = jsonObject.toString();
        jsonFormattedString = jsonResp.replaceAll("\\\\","");
        Log.d("NEW JSON", jsonFormattedString);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

The output for this is :-

{"result":["{"doc_no":"ES101","itembarcode":"BRMS","net_wt":"10","gross_wt":"1","stone_wt":"0","stone_amt":"0","rate":"32000","making":"100","qty":"1","net_rate":"32100.0","item_total":"32100.0","sum_total":"64600.0"}",
 "{"doc_no":"ES101","itembarcode":"MSAA0015","net_wt":"10","gross_wt":"11","stone_wt":"100000","stone_amt":"1","rate":"32000","making":"500","qty":"1","net_rate":"32500.0","item_total":"32500.0","sum_total":"64600.0"}"]}

But my desired output should be something like :-

[{"doc_no":"IN1001","itembarcode":"BRMS123456\nFLT22K","net_wt":"10","gross_wt":"10","stone_amt":"0","rate":"29000","making":"999","qty":"1","net_rate":"29999.0","item_total":"29999.0","sum_total":"30299.0","stone_wt":"0"},
 {"doc_no":"IN1001","itembarcode":"BRMS\nGA24K","net_wt":"10","gross_wt":"1","stone_amt":"0","rate":"32000","making":"100","qty":"1","net_rate":"","item_total":"","sum_total":"30299.0","stone_wt":""}]

How can I achieve it? Any suggestion or help is appreciated.Thank You.

Actually you don't need the following line:

jsonObject.put("result", jsonArray);

Just use the existing jsonArray like the following:

String jsonResp = jsonArray.toString();

One other note. you will get extra " " in your response and that is because of jsonArray.put(i, string); statement in the for loop which inserts extra " ". you can simply use the following to fix that:

    jsonResp = jsonResp.replaceAll("\"[{]", "{");
    jsonResp = jsonResp.replaceAll("[}]\"", "}");

Make a Model like This DocInfoModel.java ->

public class DocInfoModel {

        @SerializedName("doc_no")
        @Expose
        private String docNo;
        @SerializedName("itembarcode")
        @Expose
        private String itembarcode;
        @SerializedName("net_wt")
        @Expose
        private String netWt;
        @SerializedName("gross_wt")
        @Expose
        private String grossWt;
        @SerializedName("stone_amt")
        @Expose
        private String stoneAmt;
        @SerializedName("rate")
        @Expose
        private String rate;
        @SerializedName("making")
        @Expose
        private String making;
        @SerializedName("qty")
        @Expose
        private String qty;
        @SerializedName("net_rate")
        @Expose
        private String netRate;
        @SerializedName("item_total")
        @Expose
        private String itemTotal;
        @SerializedName("sum_total")
        @Expose
        private String sumTotal;
        @SerializedName("stone_wt")
        @Expose
        private String stoneWt;

        /**
         *
         * @return
         * The docNo
         */
        public String getDocNo() {
            return docNo;
        }

        /**
         *
         * @param docNo
         * The doc_no
         */
        public void setDocNo(String docNo) {
            this.docNo = docNo;
        }

        /**
         *
         * @return
         * The itembarcode
         */
        public String getItembarcode() {
            return itembarcode;
        }

        /**
         *
         * @param itembarcode
         * The itembarcode
         */
        public void setItembarcode(String itembarcode) {
            this.itembarcode = itembarcode;
        }

        /**
         *
         * @return
         * The netWt
         */
        public String getNetWt() {
            return netWt;
        }

        /**
         *
         * @param netWt
         * The net_wt
         */
        public void setNetWt(String netWt) {
            this.netWt = netWt;
        }

        /**
         *
         * @return
         * The grossWt
         */
        public String getGrossWt() {
            return grossWt;
        }

        /**
         *
         * @param grossWt
         * The gross_wt
         */
        public void setGrossWt(String grossWt) {
            this.grossWt = grossWt;
        }

        /**
         *
         * @return
         * The stoneAmt
         */
        public String getStoneAmt() {
            return stoneAmt;
        }

        /**
         *
         * @param stoneAmt
         * The stone_amt
         */
        public void setStoneAmt(String stoneAmt) {
            this.stoneAmt = stoneAmt;
        }

        /**
         *
         * @return
         * The rate
         */
        public String getRate() {
            return rate;
        }

        /**
         *
         * @param rate
         * The rate
         */
        public void setRate(String rate) {
            this.rate = rate;
        }

        /**
         *
         * @return
         * The making
         */
        public String getMaking() {
            return making;
        }

        /**
         *
         * @param making
         * The making
         */
        public void setMaking(String making) {
            this.making = making;
        }

        /**
         *
         * @return
         * The qty
         */
        public String getQty() {
            return qty;
        }

        /**
         *
         * @param qty
         * The qty
         */
        public void setQty(String qty) {
            this.qty = qty;
        }

        /**
         *
         * @return
         * The netRate
         */
        public String getNetRate() {
            return netRate;
        }

        /**
         *
         * @param netRate
         * The net_rate
         */
        public void setNetRate(String netRate) {
            this.netRate = netRate;
        }

        /**
         *
         * @return
         * The itemTotal
         */
        public String getItemTotal() {
            return itemTotal;
        }

        /**
         *
         * @param itemTotal
         * The item_total
         */
        public void setItemTotal(String itemTotal) {
            this.itemTotal = itemTotal;
        }

        /**
         *
         * @return
         * The sumTotal
         */
        public String getSumTotal() {
            return sumTotal;
        }

        /**
         *
         * @param sumTotal
         * The sum_total
         */
        public void setSumTotal(String sumTotal) {
            this.sumTotal = sumTotal;
        }

        /**
         *
         * @return
         * The stoneWt
         */
        public String getStoneWt() {
            return stoneWt;
        }

        /**
         *
         * @param stoneWt
         * The stone_wt
         */
        public void setStoneWt(String stoneWt) {
            this.stoneWt = stoneWt;
        }

    }

And Parse the json by GSON ->

Gson gson = new Gson();
DocInfoModel[] docModel = gson.fromJson(RESPONSE_STRING,DocInfoModel[].class);

I would suggest to create Model class for your GSON implementation.

Check out this solution.

private void testDoc()
    {
        String json = "{\"result\":[{\"doc_no\":\"ES101\",\"itembarcode\":\"BRMS\",\"net_wt\":\"10\",\"gross_wt\":\"1\",\"stone_wt\":\"0\",\"stone_amt\":\"0\",\"rate\":\"32000\",\"making\":\"100\",\"qty\":\"1\",\"net_rate\":\"32100.0\",\"item_total\":\"32100.0\",\"sum_total\":\"64600.0\"},{\"doc_no\":\"ES101\",\"itembarcode\":\"MSAA0015\",\"net_wt\":\"10\",\"gross_wt\":\"11\",\"stone_wt\":\"100000\",\"stone_amt\":\"1\",\"rate\":\"32000\",\"making\":\"500\",\"qty\":\"1\",\"net_rate\":\"32500.0\",\"item_total\":\"32500.0\",\"sum_total\":\"64600.0\"}]}";
        Gson gson = new Gson();
        DocInfo docInfo = gson.fromJson(json, DocInfo.class);

        System.out.println("Before ***********************");
        System.out.println(gson.toJson(docInfo));

        for(Result result : docInfo.getResult())
        {
            result.setDocNo("New Doc No");
        }

        System.out.println("After ***********************");
        System.out.println(gson.toJson(docInfo));
    }

DocInfo.java

import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class DocInfo {

    @SerializedName("result")
    @Expose
    private List<Result> result = new ArrayList<Result>();

    /**
     *
     * @return
     * The result
     */
    public List<Result> getResult() {
        return result;
    }

    /**
     *
     * @param result
     * The result
     */
    public void setResult(List<Result> result) {
        this.result = result;
    }

}

Result.java

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

public class Result {

    @SerializedName("doc_no")
    @Expose
    private String docNo;
    @SerializedName("itembarcode")
    @Expose
    private String itembarcode;
    @SerializedName("net_wt")
    @Expose
    private String netWt;
    @SerializedName("gross_wt")
    @Expose
    private String grossWt;
    @SerializedName("stone_wt")
    @Expose
    private String stoneWt;
    @SerializedName("stone_amt")
    @Expose
    private String stoneAmt;
    @SerializedName("rate")
    @Expose
    private String rate;
    @SerializedName("making")
    @Expose
    private String making;
    @SerializedName("qty")
    @Expose
    private String qty;
    @SerializedName("net_rate")
    @Expose
    private String netRate;
    @SerializedName("item_total")
    @Expose
    private String itemTotal;
    @SerializedName("sum_total")
    @Expose
    private String sumTotal;

    /**
     *
     * @return
     * The docNo
     */
    public String getDocNo() {
        return docNo;
    }

    /**
     *
     * @param docNo
     * The doc_no
     */
    public void setDocNo(String docNo) {
        this.docNo = docNo;
    }

    /**
     *
     * @return
     * The itembarcode
     */
    public String getItembarcode() {
        return itembarcode;
    }

    /**
     *
     * @param itembarcode
     * The itembarcode
     */
    public void setItembarcode(String itembarcode) {
        this.itembarcode = itembarcode;
    }

    /**
     *
     * @return
     * The netWt
     */
    public String getNetWt() {
        return netWt;
    }

    /**
     *
     * @param netWt
     * The net_wt
     */
    public void setNetWt(String netWt) {
        this.netWt = netWt;
    }

    /**
     *
     * @return
     * The grossWt
     */
    public String getGrossWt() {
        return grossWt;
    }

    /**
     *
     * @param grossWt
     * The gross_wt
     */
    public void setGrossWt(String grossWt) {
        this.grossWt = grossWt;
    }

    /**
     *
     * @return
     * The stoneWt
     */
    public String getStoneWt() {
        return stoneWt;
    }

    /**
     *
     * @param stoneWt
     * The stone_wt
     */
    public void setStoneWt(String stoneWt) {
        this.stoneWt = stoneWt;
    }

    /**
     *
     * @return
     * The stoneAmt
     */
    public String getStoneAmt() {
        return stoneAmt;
    }

    /**
     *
     * @param stoneAmt
     * The stone_amt
     */
    public void setStoneAmt(String stoneAmt) {
        this.stoneAmt = stoneAmt;
    }

    /**
     *
     * @return
     * The rate
     */
    public String getRate() {
        return rate;
    }

    /**
     *
     * @param rate
     * The rate
     */
    public void setRate(String rate) {
        this.rate = rate;
    }

    /**
     *
     * @return
     * The making
     */
    public String getMaking() {
        return making;
    }

    /**
     *
     * @param making
     * The making
     */
    public void setMaking(String making) {
        this.making = making;
    }

    /**
     *
     * @return
     * The qty
     */
    public String getQty() {
        return qty;
    }

    /**
     *
     * @param qty
     * The qty
     */
    public void setQty(String qty) {
        this.qty = qty;
    }

    /**
     *
     * @return
     * The netRate
     */
    public String getNetRate() {
        return netRate;
    }

    /**
     *
     * @param netRate
     * The net_rate
     */
    public void setNetRate(String netRate) {
        this.netRate = netRate;
    }

    /**
     *
     * @return
     * The itemTotal
     */
    public String getItemTotal() {
        return itemTotal;
    }

    /**
     *
     * @param itemTotal
     * The item_total
     */
    public void setItemTotal(String itemTotal) {
        this.itemTotal = itemTotal;
    }

    /**
     *
     * @return
     * The sumTotal
     */
    public String getSumTotal() {
        return sumTotal;
    }

    /**
     *
     * @param sumTotal
     * The sum_total
     */
    public void setSumTotal(String sumTotal) {
        this.sumTotal = sumTotal;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM