Android解析json树

时间:2014-02-18 10:39:36

标签: android json recursion tree

我有树JSON结构数据。 像

这样的东西
{
"result": [
    {
        "id": 1,
        "name": "test1"
    },
    {
        "id": 2,
        "name": "test12",
        "children": [
            {
                "id": 3,
                "name": "test123",
                "children": [
                    {
                        "id": 4,
                        "name": "test123"
                    }
                ]
            }
        ]
    }
]

}

模型:

class DataEntity {
    int id;
    String name;
    List<DataEntity> childDataEntity;
}

通过org.json解析

    List<DataEntity> categories = new ArrayList<DataEntity>();

private List<DataEntity> recursivellyParse(DataEntity entity, JSONObject object) throws JSONException {
    entity.setId(object.getInt("id"));
    entity.setName(object.getString("name"));
    if (object.has("children")) {
        JSONArray children = object.getJSONArray("children");
        for (int i = 0; i < children.length(); i++) {
            entity.setChildDataEntity(recursivellyParse(new DataEntity(), children.getJSONObject(i)));
            categories.add(entity);
        }
    }
    return categories;
}

呼叫

  JSONObject jsonObject = new JSONObject(JSON);
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        for (int i = 0; i < jsonArray.length(); i++) {
            recursivellyParse(new DataEntity(), jsonArray.getJSONObject(i));
        }

但这种方式是错误的。执行完方法后,List填写了相同的数据。

我该如何解析它?

UPD:更新JSON。

2 个答案:

答案 0 :(得分:1)

忽略您显示的JSON无效(我将假设这是一个复制/粘贴问题或拼写错误),问题是您已将categories列表声明为任何对象的成员是。

每次调用recursivellyParse()时都会不断添加,并且数据仍然在列表中。循环中的每个后续调用都会看到之前调用的任何内容。

编写代码时,一个简单的解决方案就是简单地添加第二个清除列表的版本:

private List<DataEntity> beginRecursivellyParse(DataEntity entity, 
                                      JSONObject object) throws JSONException {

    categories.clear();
    return recursivellyParse(entity, object);
}

然后从你的循环中调用它。

答案 1 :(得分:1)

以下是完整演示如何根据需要解析json数据。

String JSON = "your json string";
  ArrayList<DataEntity> finalResult = new ArrayList<>();


  try {
                JSONObject main = new JSONObject(JSON);

                JSONArray result = main.getJSONArray("result");

                for(int i=0;i<result.length();i++){
                    DataEntity dataEntity = parseObject(result.getJSONObject(i));
                    finalResult.add(dataEntity);
                }

                Log.d("DONE","Done Success");

            } catch (JSONException e) {
                e.printStackTrace();
            }

创建一个递归函数来解析对象。

public DataEntity parseObject(JSONObject dataEntityObject) throws JSONException {
    DataEntity dataEntity = new DataEntity();
    dataEntity.id = dataEntityObject.getString("id");
    dataEntity.name = dataEntityObject.getString("name");

    if(dataEntityObject.has("children")){
        JSONArray array = dataEntityObject.getJSONArray("children");
        for(int i=0;i<array.length();i++){
            JSONObject jsonObject = array.getJSONObject(i);
            DataEntity temp = parseObject(jsonObject);
            dataEntity.children.add(temp);
        }
    }

    return dataEntity;
}

模型类

public class DataEntity implements Serializable {
public String id = "";
public String name = "";
ArrayList<DataEntity> children = new ArrayList<>();}

在FinalResult Arraylist中,您将获得所有解析数据。