1

I have tree JSON-structured data. Something like

{
"result": [
    {
        "id": 1,
        "name": "test1"
    },
    {
        "id": 2,
        "name": "test12",
        "children": [
            {
                "id": 3,
                "name": "test123",
                "children": [
                    {
                        "id": 4,
                        "name": "test123"
                    }
                ]
            }
        ]
    }
]

}

model:

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

Parsing via 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;
}

call

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

But this way is wrong. After execution of the method List filled out same data.

How do I parse it right?

UPD: update JSON.

2
  • 1
    your response is invalid , check here jsonlint.com Commented Feb 18, 2014 at 10:45
  • @YuriyAizenberg: I am facing the same problem.. I did not get the solution written below.. can you please tell me what changes u made to make it work ? Commented Dec 15, 2014 at 10:34

2 Answers 2

2

Here is Full Demo How to Parse json data as you want.

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();
            }

Create One recursive function to parse object.

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;
}

Model Class

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

In FinalResult Arraylist you will get all your parse data.

Sign up to request clarification or add additional context in comments.

1 Comment

While your answer may be correct, it would be better if you also pointed out what was wrong with the poster's code. Note that the accepted answer was much more relevant to this question.
1

Ignoring that the JSON you show is invalid (i'm going to assume that's a copy/paste problem or typo), the issue is that you've declared your categories List as a member of whatever object that is.

It's continually getting added to on every call to recursivellyParse() and that data remains in the list. Each subsequent call from your loop is seeing whatever previous calls put in it.

A simple solution to this as your code is written would be to simply add a second version that clears the list:

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

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

Then call that from your loop.

3 Comments

Yes, I understand that the problem in the recursive call. But do not quite understand how to do it right. Could you show me the correct version?
@YuriyAizenberg see edit; that's one way to solve it
@YuriyAizenberg and Brian Roach: I have try your answer but i got error while storing data in ArrayList. Can you please give me solution for my problem. I have put my code in below link : app.box.com/s/9ca7bhp04y078wduumoj7j0wkaa4rw40 Thankx in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.