我需要从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 中获取所有数据。