bluelinelabs / LoganSquare

Screaming fast JSON parsing and serialization library for Android.
Apache License 2.0
3.21k stars 306 forks source link

Parse JSONObject in List #219

Closed bluelancer closed 6 years ago

bluelancer commented 7 years ago

Dear all,

I have a JSON looks like:

{"objects": 
    [
        {
            "Board 1_0": 
                {
                    "detectionPercentage":"70",
                    "top":"93",
                    "bottom":"110",
                    "left":"195",
                    "right":"203"
                }
        },
        {
            "Board 1_1": 
                {
                    "detectionPercentage":"61",
                    "top":"90",
                    "bottom":"112",
                    "left":"309",
                    "right":"314"
                }
        }
    ] 
}

As objects in an array. I tried

@JsonObject
public class objectinfo {

    @JsonObject
     public static class Board_1_0{

        @JsonField (name = "Board 1_0")
        public String board_name;

        @JsonObject
        public static class features{

            @JsonField
            public static String detectionPercentage;
            @JsonField
            public String top;

            @JsonField
            public String bottom;

            @JsonField
            public String left;

            @JsonField
            public String right;

        }
    }

    @JsonField(name = "objects")
    public String object;

    @JsonField (name = "name")
    public String id;

    @JsonField(name = "ID")
    public String name;

    public int nonJsonField;

    @JsonField
    private int privateInt;

    public int getPrivateInt() {
        return privateInt;
    }

    public void setPrivateInt(int i) {
        privateInt = i;
    }
}

and tried to grab detectionPercentage, yet it is not working, what is wrong?

colemcallister commented 6 years ago

If you have any way to change the JSON that is coming to you, I would recommend making it so that it sends a list of boards, and the boards have a "name" property in which you can put "Board 1_1" or "Board 1_0". That will make it so that you can be more dynamic. The problem you are running into is that you have a property name that can be dynamic. That's why I propose what I said before. If you must deal with that JSON that way you can do the following:

@JsonObject
public class Boards {

    @JsonField(name = "objects")
    List<BoardContainer> boardContainers;

    public List<BoardContainer> getBoardContainers() {
        return boardContainers;
    }

    public void setBoardContainers(List<BoardContainer> boardContainers) {
        this.boardContainers = boardContainers;
    }
}
@JsonObject
public class BoardContainer {
    @JsonField(name = "Board 1_0")
    private Board board_1_0;

    @JsonField(name = "Board 1_1")
    private Board board_1_1;

    public Board getBoard_1_0() {
        return board_1_0;
    }

    public void setBoard_1_0(Board board_1_0) {
        this.board_1_0 = board_1_0;
    }

    public Board getBoard_1_1() {
        return board_1_1;
    }

    public void setBoard_1_1(Board board_1_1) {
        this.board_1_1 = board_1_1;
    }
}
@JsonObject
public class Board {
    @JsonField
    private String detectionPercentage;

    @JsonField
    private String top;

    @JsonField
    private String bottom;

    @JsonField
    private String left;

    @JsonField
    private String right;

    public String getDetectionPercentage() {
        return detectionPercentage;
    }

    public void setDetectionPercentage(String detectionPercentage) {
        this.detectionPercentage = detectionPercentage;
    }

    public String getTop() {
        return top;
    }

    public void setTop(String top) {
        this.top = top;
    }

    public String getBottom() {
        return bottom;
    }

    public void setBottom(String bottom) {
        this.bottom = bottom;
    }

    public String getLeft() {
        return left;
    }

    public void setLeft(String left) {
        this.left = left;
    }

    public String getRight() {
        return right;
    }

    public void setRight(String right) {
        this.right = right;
    }
}
bluelancer commented 6 years ago

ThX!