Search code examples
androidarraysjsonjsonparser

JSON Parsing data from 2 dimension array


I need to get data from house array, My json file looks like,

{
"mydata": {
"totalRoads": "13",
"noOfHouse": "5",
"house": [
  {
    "road": "1",
    "right": [
      {
        "houseID": "A3",
        "isPainted": "false",
        "ownerGender": "female"
      },
      {
        "houseID": "A4",
        "isPainted": "true",
        "ownerGender": "female"
      }
    ],
    "left": [
      {
        "houseID": "A1",
        "isPainted": "false",
        "ownerGender": "female"
      },
      {
        "houseID": "A2",
        "isPainted": "false",
        "ownerGender": "female"
      }
    ]
  },
  {
    "road": "2",
    "right": [
      {
        "houseID": "B3",
        "isPainted": "false",
        "ownerGender": "male"
      },
      {
        "houseID": "B4",
        "isPainted": "true",
        "ownerGender": "male"
      }
    ],
    "left": [
      {
        "houseID": "B1",
        "isPainted": "true",
        "ownerGender": "male"
      },
      {
        "houseID": "B2",
        "isPainted": "true",
        "ownerGender": "male"
      }
    ]
  },
  {
    "road": "3",
    "right": [
      {
        "houseID": "C3",
        "isPainted": "false",
        "ownerGender": "male"
      },
      {
        "houseID": "C4",
        "isPainted": "false",
        "ownerGender": "male"
      }
    ],
    "left": [
      {
        "houseID": "C1",
        "isPainted": "true",
        "ownerGender": "male"
      },
      {
        "houseID": "C2",
        "isPainted": "false",
        "ownerGender": "male"
      }
    ]
  }
]
}
}

I just tried like this to parse data from json,

InputStream inputStream = getResources().openRawResource(R.raw.house_details);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ctr;
    try {
        ctr = inputStream.read();
        while (ctr != -1) {
            byteArrayOutputStream.write(ctr);
            ctr = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.v("House Data", byteArrayOutputStream.toString());
    try {
        JSONObject jObject = new JSONObject(
                byteArrayOutputStream.toString());
        JSONObject jObjectResult = jObject.getJSONObject("mydata");
        JSONArray jArray = jObjectResult.getJSONArray("house");
        String house_ID = "";
        boolean is_painted = false;
        String owner_gender = "";
        ArrayList<String[]> data = new ArrayList<String[]>();
        for (int i = 0; i < jArray.length(); i++) {
            house_ID = jArray.getJSONObject(i).getString("houseID");
            is_painted = jArray.getJSONObject(i).getBoolean("isPainted");
            owner_gender = jArray.getJSONObject(i).getString("ownerGender");
            Log.v("house_ID", house_ID);
            Log.v("is_painted", String.valueOf(is_painted));
            Log.v("owner_gender", owner_gender);
            data.add(new String[] { house_ID, String.valueOf(is_painted), owner_gender });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

But I couldn't get what I am expecting, Please help me get all data from json.


Solution

  • You can use Gson lib to get the data from Json easy way and your structure will be like this

    public class Response{
    
        @SerializedName("mydata")
        private Mydata mydata;
    
        public void setMydata(Mydata mydata){
            this.mydata = mydata;
        }
    
        public Mydata getMydata(){
            return mydata;
        }
    
        @Override
        public String toString(){
            return 
                "Response{" + 
                "mydata = '" + mydata + '\'' + 
                "}";
            }
    }
    
    public class Mydata{
    
        @SerializedName("totalRoads")
        private String totalRoads;
    
        @SerializedName("noOfHouse")
        private String noOfHouse;
    
        @SerializedName("house")
        private List<HouseItem> house;
    
        public void setTotalRoads(String totalRoads){
            this.totalRoads = totalRoads;
        }
    
        public String getTotalRoads(){
            return totalRoads;
        }
    
        public void setNoOfHouse(String noOfHouse){
            this.noOfHouse = noOfHouse;
        }
    
        public String getNoOfHouse(){
            return noOfHouse;
        }
    
        public void setHouse(List<HouseItem> house){
            this.house = house;
        }
    
        public List<HouseItem> getHouse(){
            return house;
        }
    
        @Override
        public String toString(){
            return 
                "Mydata{" + 
                "totalRoads = '" + totalRoads + '\'' + 
                ",noOfHouse = '" + noOfHouse + '\'' + 
                ",house = '" + house + '\'' + 
                "}";
            }
    }
    public class HouseItem{
    
        @SerializedName("road")
        private String road;
    
        @SerializedName("left")
        private List<LeftItem> left;
    
        @SerializedName("right")
        private List<RightItem> right;
    
        public void setRoad(String road){
            this.road = road;
        }
    
        public String getRoad(){
            return road;
        }
    
        public void setLeft(List<LeftItem> left){
            this.left = left;
        }
    
        public List<LeftItem> getLeft(){
            return left;
        }
    
        public void setRight(List<RightItem> right){
            this.right = right;
        }
    
        public List<RightItem> getRight(){
            return right;
        }
    
        @Override
        public String toString(){
            return 
                "HouseItem{" + 
                "road = '" + road + '\'' + 
                ",left = '" + left + '\'' + 
                ",right = '" + right + '\'' + 
                "}";
            }
    }