mohan babu
mohan babu

Reputation: 1448

JSON mapping to Java returning null value

I'm trying to map JSON to Java using gson.I was succesful in writing the logic but unsuccesful in getting the output.Below posted are my JSON and Java files.Any help would be highly appreciated.

This is the output i'm getting

value:null

Below posted is the code for .json files

{
    "catitem": {
        "id": "1.196289",
        "src": "http://feeds.reuters.com/~r/reuters/MostRead/~3/PV-SzW7Pve0/story06.htm",
        "orig_item_date": "Tuesday 16 June 2015 07:01:02 PM UTC",
        "cat_id": "1",
        "heding": "Putin says Russia beefing up nuclear arsenal",
        "summary": "KUvdfbefb bngfb",
        "body": {
            "bpart": [
                "KUBINKA,dvdvdvdvgbtgfdnhfbnrtdfbcv dbnfg"
            ]
        }
    }
}

Below posted is my .java file

public class offc {

    public static void main(String[] args) {
        JsonReader jr = null;

        try {
            jr = new JsonReader(new InputStreamReader(new FileInputStream(
                    "C:\\Users\\rishii\\IdeaProjects\\rishi\\src\\file3.json")));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Doll s = new Doll();

        Gson g = new Gson();
        Doll sr1 = g.fromJson(jr, Doll.class);
        System.out.println(sr1);
    }
}

Below posted is the code for Doll.java

class Doll {

    private catitem ct;

    public void setCt(catitem ct) {

        this.ct = ct;
    }

    public catitem getCt() {
        return ct;
    }

    @Override
    public String toString()

    {
        return "value:" + ct;
    }

    class catitem {
        private String id;
        private String src;
        private String orig_item_date;
        private String cat_id;
        private String heding;
        private String summary;
        private body ber;

        catitem(String id, String src, String orig_item_date, String cat_id, String heding,
                String summary) {
            this.id = id;
            this.src = src;
            this.orig_item_date = orig_item_date;
            this.cat_id = cat_id;
            this.heding = heding;
            this.summary = summary;
        }

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

        public String getId() {
            return id;
        }

        public void setSrc(String src) {
            this.src = src;
        }

        public String getSrc() {
            return src;
        }

        public void setOrig_item_date(String Orig_item_date) {
            this.orig_item_date = Orig_item_date;
        }

        public String getOrig_item_date() {

            return getOrig_item_date();
        }

        public void setCat_id(String cat_id) {

            this.cat_id = cat_id;
        }

        public String getCat_id() {
            return cat_id;
        }

        public void setHeding(String heding) {
            this.heding = heding;
        }

        public String getHeding() {
            return heding;
        }

        public void setSummary(String summary) {
            this.summary = summary;
        }

        public String getSummary() {
            return summary;
        }

        public void setBer(body ber) {
            this.ber = ber;
        }

        public body getBer() {
            return ber;
        }

        @Override
        public String toString() {
            return "id:" + id + "cat_id" + cat_id + "summary" + summary + "orig_date"
                    + orig_item_date + "heding" + heding;
        }
    }

    class body {
        private String bpart;

        public void setBpart(String r) {

            this.bpart = r;
        }

        public String getBpart() {

            return bpart;
        }

        @Override
        public String toString() {

            return "hiii";
        }
    }

}

Upvotes: 0

Views: 212

Answers (1)

barunsthakur
barunsthakur

Reputation: 1216

The issue is in class Doll, You have a field ct but in json catitem. Rename the field ct to catitem or if you are using Gson use @SerializedName("catitem") on filed ct and it will work.

Upvotes: 1

Related Questions