0

I have got a problem using JSON parser in an android application. I'm starting with this platform, and would like to:

  • get some values (different types) from a JSON API
  • init a custom class with theses values easily

So, in the iOS application, I made it with a NSDictionnary to set the values to my class. Is it possible to do a such thing in Java ?

I have my User class :

public class User {
    protected String id;
    protected GregorianCalendar registredAt;
    protected String username;
    protected String email;
    protected Integer score;
    protected Integer bonus;
    protected Integer sessionsOpened;
    protected Integer sessionsWon;
    protected Team team;
    protected String avatar;
}

Of course I could create a big constructor with every values...

public User(String id, GregorianCalendar registredAt, String username, String email, Integer score, Integer bonus, Integer sessionsOpened, Integer sessionsWon, Team team, String avatar) {
    this.id = id;
    this.registredAt = registredAt;
    ...
}

But, to do it, I must manage my JSONObject before to init my User class... What I would do is to initialize this class with the an array of multi-typed values that I could get from the JSONObject... ( Convert Json Array to normal Java Array )

Is it possible ? What is the best method to get a clean array from a JSONObject or JSONArray ?

If it is not possible, another way could be to create the User directly with the JSONObject...

public User(JSONObject attributes) {

}

But I would prefer the java array if that is possible.

Thank you in advance, Bobby.

Community
  • 1
  • 1
Flo Schild
  • 5,104
  • 4
  • 40
  • 55
  • Have a look at `java.util.HashMap` - something like `NSDictionary` – Aleks G Jul 04 '12 at 16:20
  • Yeah I just saw it :) But is it possible to set Objects in a HashMap, as GregorianCalendar or Team (another Class) ? – Flo Schild Jul 04 '12 at 16:35
  • 1
    Yes, a `Map` can contain any object types as keys and any object types as values, as long as it's declared properly (in the generics sense), e.g. `Map,?> data = new HashMap,?>();` – Aleks G Jul 05 '12 at 07:39
  • Okay thank you, it's good to know. I will finally use the jackson library to parse my API Json callbacks. – Flo Schild Jul 05 '12 at 07:52

0 Answers0