0

actually I'm struggeling with parsing the following JSON doc: LINK

I have a class which is saving the content in a String (rawData). After that I want to parse it with Gson.

DownloadedCategories dcats = new Gson().fromJson(rawData, DownloadedCategories.class);

My goal is to have a List of an extra datatype of the 21 main categories, and in the extra datatype saved in another List the subcategories.

My approach was to create the new datatype mainCategory which includes the List of the subcategories.

The problem is that i can't do the DownloadedCategories class like this:

public class DownloadedCategories
{
    private List<mainCategories> categories;

    public List<mainCategories> getCategories;
    {
        return categories;
    }
}

Has someone an idea how to fix my issue?

TacoVox
  • 141
  • 10
  • 1
    nobody will ever know what your actual issue is - without error messages, problem descriptions and the likes – specializt Dec 11 '14 at 19:00
  • @TacoVox, please explain this more clearly. Very hard to understand what you're trying to do and what trouble you're having. – Jonathan M Dec 11 '14 at 19:04
  • The thing is that I don't know how to get the main categories into an object. – TacoVox Dec 11 '14 at 19:17
  • 1
    You're starting with the wrong paradigm. Before you get into "POJOs", you should start with a simple parser that parses into Maps and Lists. When you understand that, *then* you can start to use the fancier stuff. Starting where you are you will always be confused. – Hot Licks Dec 11 '14 at 19:44

1 Answers1

-1

Looks like that Json from your link is not formatted to fit in your object.

You need to know in which attribute you will put your categories since you are trying to parse a Json array in an object. In your DownloadedCategories the attribute is categories. So you need to wrap your Json in an attribute categories.

    String wellFolrmedJson = "{\"categories\": " + rawData + "}";
    DownloadedCategories dcats = new Gson().fromJson(wellFolrmedJson , DownloadedCategories.class);

Gson will bind the json array in your Object list.

The best way to parse your data is by using array as starting point like in this example.

mainCategories[] dcats = new Gson().fromJson(rawData , mainCategories[].class);
Community
  • 1
  • 1
bodyjares
  • 440
  • 1
  • 4
  • 16