我需要从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获取所有数据。
答案 0 :(得分:2)
你的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"
}
]
}
]
}
}
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++) {
JSONObject obj= jArray.getJSONObject(i);
JSONArray right = obj.getJSONArray("right");
JSONArray left= obj.getJSONArray("left");
for(int j=0;j<right.length();j++)
{
house_ID = right.getJSONObject(j).getString("houseID");
is_painted = right.getJSONObject(j).getBoolean("isPainted");
owner_gender = right.getJSONObject(j).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 });
}
for(int j=0;j<left.length();j++)
{
house_ID = left.getJSONObject(j).getString("houseID");
is_painted = left.getJSONObject(j).getBoolean("isPainted");
owner_gender = left.getJSONObject(j).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();
}
答案 1 :(得分:2)
你可以使用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 + '\'' +
"}";
}
}
答案 2 :(得分:2)
1-为houseData创建一个模型,即
public class HotelEntity {
private String houseID;
private String isPainted;
private String ownerGender;
// add getter and setter here
}
2-从每个房屋对象的左右分析数据
for (int i = 0; i < jArray.length(); i++) {
//get the data and map it object
Array.getJSONObject(i).getString("right");
for {
// add all models to list
}
Array.getJSONObject(i).getString("left");
for {
// add all models to list
}
}
3将它添加到houseEntity的ArrayList中。
此外,您可以使用Gson进行json对象转换。 您不必手动解析json中的数据。
答案 3 :(得分:2)
您可以使用
从json String创建对象http://www.jsonschema2pojo.org/
----------------------------------- com.example.Example.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("mydata")
@Expose
private Mydata mydata;
public Mydata getMydata() {
return mydata;
}
public void setMydata(Mydata mydata) {
this.mydata = mydata;
}
}
----------------------------------- com.example.House.java ----- ------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class House {
@SerializedName("road")
@Expose
private String road;
@SerializedName("right")
@Expose
private List<Right> right = null;
@SerializedName("left")
@Expose
private List<Left> left = null;
public String getRoad() {
return road;
}
public void setRoad(String road) {
this.road = road;
}
public List<Right> getRight() {
return right;
}
public void setRight(List<Right> right) {
this.right = right;
}
public List<Left> getLeft() {
return left;
}
public void setLeft(List<Left> left) {
this.left = left;
}
}
----------------------------------- com.example.Left.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Left {
@SerializedName("houseID")
@Expose
private String houseID;
@SerializedName("isPainted")
@Expose
private String isPainted;
@SerializedName("ownerGender")
@Expose
private String ownerGender;
public String getHouseID() {
return houseID;
}
public void setHouseID(String houseID) {
this.houseID = houseID;
}
public String getIsPainted() {
return isPainted;
}
public void setIsPainted(String isPainted) {
this.isPainted = isPainted;
}
public String getOwnerGender() {
return ownerGender;
}
public void setOwnerGender(String ownerGender) {
this.ownerGender = ownerGender;
}
}
----------------------------------- com.example.Mydata.java ----- ------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Mydata {
@SerializedName("totalRoads")
@Expose
private String totalRoads;
@SerializedName("noOfHouse")
@Expose
private String noOfHouse;
@SerializedName("house")
@Expose
private List<House> house = null;
public String getTotalRoads() {
return totalRoads;
}
public void setTotalRoads(String totalRoads) {
this.totalRoads = totalRoads;
}
public String getNoOfHouse() {
return noOfHouse;
}
public void setNoOfHouse(String noOfHouse) {
this.noOfHouse = noOfHouse;
}
public List<House> getHouse() {
return house;
}
public void setHouse(List<House> house) {
this.house = house;
}
}
----------------------------------- com.example.Right.java ----- ------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Right {
@SerializedName("houseID")
@Expose
private String houseID;
@SerializedName("isPainted")
@Expose
private String isPainted;
@SerializedName("ownerGender")
@Expose
private String ownerGender;
public String getHouseID() {
return houseID;
}
public void setHouseID(String houseID) {
this.houseID = houseID;
}
public String getIsPainted() {
return isPainted;
}
public void setIsPainted(String isPainted) {
this.isPainted = isPainted;
}
public String getOwnerGender() {
return ownerGender;
}
public void setOwnerGender(String ownerGender) {
this.ownerGender = ownerGender;
}
}
你可以获得一切:
JSONObject jObject = new JSONObject(
byteArrayOutputStream.toString());
Gson g = new Gson();
Example example = g.fromJson(jObject .toString(), Example.class);
您可以从示例中获取价值。
Mydata mydata = example.getMyData();
List<House> lstHouse = mydata.getHouse();
for (i= 0; i < lstHouse.size(); i++){
House house = lstHouse.get(i);
String road = house.getRoad();
List<Right> lstRight = house.getRight();
for (j= 0; j < lstRight .size(); j++){
Right right = lstRight.get(j);
String houseID = right.getHouseID();
String isPainted = right.getIsPainted();
String ownerGender = right.getOwnerGender();
}
List<Left> lstLeft = house.getLeft();
for (k= 0; k < lstLeft .size(); k++){
Left left= lstLeft .get(k);
String houseID = left.getHouseID();
String isPainted = left.getIsPainted();
String ownerGender = left.getOwnerGender();
}
}
我希望它可以帮助你解决问题!
答案 4 :(得分:1)
您尝试访问house
到houseId
的数据,但之前必须通过right
和left
结构。
答案 5 :(得分:1)
house_ID = jArray.getJSONObject(i).getString(“houseID”); //错误的方式
我猜JSONException
会发生。
您应该创建两个 FOR loop
。
for (int i = 0; i < jArray.length(); i++)
{
JSONObject _jOBJ = jArray.getJSONObject(i);
JSONArray jsonArray = _jOBJ.getJSONArray("right");
for (int j = 0; j < jsonArray.length(); j++)
{
JSONObject _jOBJCHILD = jsonArray.getJSONObject(j);
String str_HOUSEID = _jOBJCHILD.getString("houseID");
}
}
答案 6 :(得分:1)
houseID在右侧数组内,不在内部数组中。
试试这个:
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++) {
JSONObject jsonObj = jArray.getJSONObject(i);
JSONArray jsonArray = jsonObj.getJSONArray("right");
for (int j = 0; j < jsonArray.length(); j++)
{
house_ID = jsonArray.getJSONObject(j).getString("houseID");
is_painted = jsonArray.getJSONObject(j).getBoolean("isPainted");
owner_gender = jsonArray.getJSONObject(j).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 });
}
}
答案 7 :(得分:1)
这是一个非常接近您的实现的解决方案,但是,正确的,并且还将读取“左”数组:)
InputStream inputStream = getResources().openRawResource(R.raw.sojson);
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 houseArray = jObjectResult.getJSONArray("house");
String house_ID = "";
boolean is_painted = false;
String owner_gender = "";
ArrayList<String[]> data = new ArrayList<String[]>();
for (int i = 0; i < houseArray.length(); i++) {
JSONObject ob = houseArray.getJSONObject(i);
JSONArray rightArray = ob.getJSONArray("right");
for (int r = 0; r < rightArray.length(); r++) {
house_ID = rightArray.getJSONObject(r).getString("houseID");
is_painted = rightArray.getJSONObject(r).getBoolean("isPainted");
owner_gender = rightArray.getJSONObject(r).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});
}
JSONArray leftArray = ob.getJSONArray("left");
for (int l = 0; l < leftArray.length(); l++) {
house_ID = leftArray.getJSONObject(l).getString("houseID");
is_painted = leftArray.getJSONObject(l).getBoolean("isPainted");
owner_gender = leftArray.getJSONObject(l).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:
{
"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"
}
]
}
]
}
}