我需要从house数组中获取数据,我的json文件如下所示:

{
"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"
      }
    ]
  }
]
}
}


我只是这样尝试解析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();
    }


但是我无法得到我所期望的,请帮助我从json获取所有数据。

最佳答案

您可以使用Gson lib从Json轻松获取数据,您的结构将如下所示

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 + '\'' +
            "}";
        }
}

10-07 16:30
查看更多