-2

Here is my Json data? how to parse in android

how to parse it

{

"data":[
{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}

]}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
B.Thakur
  • 61
  • 1
  • 8
  • did you get it solved from answers given by GSON or you need a detailed structure example of how to parse ARRAY and OBJECTS in json? – Adam Feb 23 '18 at 12:17

2 Answers2

0

Google offers a very good json parser GSON. Take a look here

0

Gson is a superb library for this. First generate the POJO for the json you have from jsonschematopojo. Here is the following POJO class:

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

public class JsonResponse {

    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

    public class Datum {

        @SerializedName("widget")
        @Expose
        private Widget widget;

        public Widget getWidget() {
            return widget;
        }

        public void setWidget(Widget widget) {
            this.widget = widget;
        }

    }

    public class Image {

        @SerializedName("src")
        @Expose
        private String src;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("hOffset")
        @Expose
        private int hOffset;
        @SerializedName("vOffset")
        @Expose
        private int vOffset;
        @SerializedName("alignment")
        @Expose
        private String alignment;

        public String getSrc() {
            return src;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHOffset() {
            return hOffset;
        }

        public void setHOffset(int hOffset) {
            this.hOffset = hOffset;
        }

        public int getVOffset() {
            return vOffset;
        }

        public void setVOffset(int vOffset) {
            this.vOffset = vOffset;
        }

        public String getAlignment() {
            return alignment;
        }

        public void setAlignment(String alignment) {
            this.alignment = alignment;
        }

    }

    public class Text {

        @SerializedName("data")
        @Expose
        private String data;
        @SerializedName("size")
        @Expose
        private int size;
        @SerializedName("style")
        @Expose
        private String style;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("hOffset")
        @Expose
        private int hOffset;
        @SerializedName("vOffset")
        @Expose
        private int vOffset;
        @SerializedName("alignment")
        @Expose
        private String alignment;
        @SerializedName("onMouseUp")
        @Expose
        private String onMouseUp;

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public String getStyle() {
            return style;
        }

        public void setStyle(String style) {
            this.style = style;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHOffset() {
            return hOffset;
        }

        public void setHOffset(int hOffset) {
            this.hOffset = hOffset;
        }

        public int getVOffset() {
            return vOffset;
        }

        public void setVOffset(int vOffset) {
            this.vOffset = vOffset;
        }

        public String getAlignment() {
            return alignment;
        }

        public void setAlignment(String alignment) {
            this.alignment = alignment;
        }

        public String getOnMouseUp() {
            return onMouseUp;
        }

        public void setOnMouseUp(String onMouseUp) {
            this.onMouseUp = onMouseUp;
        }

    }

    public class Widget {

        @SerializedName("debug")
        @Expose
        private String debug;
        @SerializedName("window")
        @Expose
        private Window window;
        @SerializedName("image")
        @Expose
        private Image image;
        @SerializedName("text")
        @Expose
        private Text text;

        public String getDebug() {
            return debug;
        }

        public void setDebug(String debug) {
            this.debug = debug;
        }

        public Window getWindow() {
            return window;
        }

        public void setWindow(Window window) {
            this.window = window;
        }

        public Image getImage() {
            return image;
        }

        public void setImage(Image image) {
            this.image = image;
        }

        public Text getText() {
            return text;
        }

        public void setText(Text text) {
            this.text = text;
        }

    }

    public class Window {

        @SerializedName("title")
        @Expose
        private String title;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("width")
        @Expose
        private int width;
        @SerializedName("height")
        @Expose
        private int height;

        public String getTitle() {
            return title;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

    }
}

Now copy the entire json data and paste it in a String variable:

        String jsonData="{\n" +
                "\n" +
                "\"data\":[ {\"widget\": { \"debug\": \"on\", \"window\": { \"title\": \"Sample Konfabulator Widget\", \"name\": \"main_window\", \"width\": 500, \"height\": 500 }, \"image\": { \"src\": \"Images/Sun.png\", \"name\": \"sun1\", \"hOffset\": 250, \"vOffset\": 250, \"alignment\": \"center\" }, \"text\": { \"data\": \"Click Here\", \"size\": 36, \"style\": \"bold\", \"name\": \"text1\", \"hOffset\": 250, \"vOffset\": 100, \"alignment\": \"center\", \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\" } }}\n" +
                "\n" +
                "]}"
        Gson gson = new Gson();
        JsonResponse jsonresp= gson.fromJson(jsonData, JsonResponse.class);

Now if you want to read from a file, then apply these codes:

public String mockDataReader(String fileName) {
    try {
        Path path = Paths.get(getClass().getClassLoader()
                .getResource(fileName).toURI());
        StringBuilder data = new StringBuilder();
        Stream<String> lines = Files.lines(path);
        lines.forEach((String line) -> data.append(line).append("\n"));
        lines.close();

        path = null;
        return data.toString();

    } catch (URISyntaxException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
sam
  • 1,800
  • 1
  • 25
  • 47
  • upvote if you find my answer helpful. If my answer solved your question then you can mark it as a solution. – sam Feb 23 '18 at 12:03