0

I'm programming one android list-detail application and I have to load app structure from json. I'm trying to deserialize non-optimal json with Gson (Java) to objects, from that I'm going to dynamically generate fragments (I hope it can be done :-) ).

I have tabs within main FragmentActivity and one tab has own fragment and own json. Every tabs has own fragmentstack for controlling with back and up button.

I have this json:

{
    "data": [],
    "children": [
        {
            "data": {
                "id": "5",
                "deep": "0",
                "url": "compare",
                "type": "navigation",
                "name": "Vergleich",
                "text": null,
                "number": null
            },
            "children": [
                {
                    "data": {
                        "id": "12",
                        "deep": "1",
                        "url": "information",
                        "type": "navigation",
                        "name": "information",
                        "text": null,
                        "number": null
                    },
                    "children": []
                },
                {
                    "data": {
                        "id": "13",
                        "deep": "1",
                        "url": "application",
                        "type": "navigation",
                        "name": "application",
                        "text": null,
                        "number": null
                    },
                    "children": []
                }
            ]
        }
    ]
}

and I created this classes for gson:

public class StructureItem {
    StructureData data;
    ArrayList<StructureItem> children;
}
public class StructureData {
    public int id;
    public int deep;
    public String url;
    public String type;
    public String name;
    public String text;
    public String number;
}

I tried to create Object with:

String s = LoadJson();
Gson gson = gsonBuilder.create();
StructureItem root = gson.fromJson(s, StructureItem.class);

But cannot succeed.

How would you solve my problem? Any easy solution?

It would be awesome has some notes for this. I'm programming for couple years, but this is my first android project. Quite monstrous framework!

SlovaN
  • 326
  • 2
  • 10
  • 1
    You've described what you tried but not what resulted. What happened when you tried to call gson.fromJson as you described? Was there a compile-time error? A runtime error? An unexpected result? Any details you can give will help people help you. (Also, your final code example needs a blank line before it in order to format correctly.) – Martin Atkins Mar 18 '13 at 00:52
  • Sorry, it was 2am and I had to go sleep. I'm going to create minor java only project for this purpose and give results. In android it is a bit uncomfortable to getting result. – SlovaN Mar 18 '13 at 20:13
  • It raised error "Expected BEGIN_OBJECT but was BEGIN_ARRAY" and it was caused by mistake in my simplified json file. After this, code run flawlessly! Thanks for your time. I was lazy to test it in separate pure java project... But this is partly caused by that I did not believe at start, that gson library can handle some json like this with no modification! – SlovaN Mar 18 '13 at 22:27
  • So did I understand correctly that you've solved the problem yourself? If so, please consider posting an answer to your own question describing what the problem was and how you solved it, for the benefit of others that might find this question in search results in future. – Martin Atkins Mar 19 '13 at 03:53
  • Ok, I am going to describe answer with description how it works and where mistake was. Maybe it will be helpfull for someone in future. – SlovaN Mar 19 '13 at 09:28

1 Answers1

0

Try to replace ArrayList<StructureItem> to StructureItem[] in class StructureItem.

John
  • 43
  • 1
  • 5
  • This is not recommended doing in Java [Array or List in Java. Which is faster?](http://stackoverflow.com/a/716641/923681) – SlovaN Mar 19 '13 at 09:32