1

I am new to Android development and JAVA. I need to pass JSONObject values into android class. I am aware of the external libraries like GSON and others (like Jackson), but for this task I mus use only JSON. So far I have created an JSONObject:

public void jsonObject(){

    JSONObject test1 = new JSONObject();
    try{
        test1.put("name", "Pete");
        test1.put("surname", "Thompson");
        test1.put("age", "24");
        test1.put("height","182");
        test1.put("id", "45");
    }
    catch (JSONException a){
        a.printStackTrace();
    }
}

From this object, I need to pass the data to the following class members:

private String name;
private String surname;
private int age;
private int height;
private long id;

Thanks in advance.

3
  • Hey exactly this is provided by gson, have a look at some tuts out there, you will love it! Commented Feb 26, 2014 at 13:13
  • Yeah I have used the GSON ext.lib., but now I have to use only built in lib. to better understand how it all works. Commented Feb 26, 2014 at 13:15
  • ooh ok... think you will get it ;) hf Commented Feb 26, 2014 at 13:21

4 Answers 4

3

Then just make appropriate constructor for your class:

public class Test {
    String name;
    String surname;
    int age;
    int height;
    long id;

    public Test(JSONObject jsonObject) {
        this.name = jsonObject.getString("name");
        this.surname = jsonObject.getString("surname");
        this.age = jsonObject.getInt("age");
        this.height = jsonObject.getInt("height");
        this.id = jsonObject.getLong("id");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

But the fields are private, the use of getters and setters is required , am I right?
@russell1911dev If you want to keep them private, yes, getters and setters are required (but please keep in mind, that they are not recommended in Android developer.android.com/training/articles/…). Or you can make them package-visible or public.
could you give me some example on how to put information from JSONObject, into getter and setter and print into console using system.out.print? I wouldn't ask, but I get nullpointerException.
@russell1911dev I've made them package-protected (with no access modifier), so you don't need getters and setters. Please note, that you cannot use System.out on Android, use Log class instead (developer.android.com/reference/android/util/Log.html)
If you get an error it's better to update your question.
1
try{    
    String name = test1.getString("name");
    String surName= test1.getString("surname");
    int age = test1.getInt("age");
    int height = test1.getInt("height");
    long id = test1.getLong("id");
    YourClass mInstance = new YourClass(name,surName,age,height,id);
}
catch(JSONParseException){
}

Comments

1

The workload when using GSON would look like this:

Object generated with GSON:

class MyUser{

    @Expose @SerializedName("name");
    private String name;

    @Expose @SerializedName("surname");
    private String surname;

    //and so on

    public String getName(){
        return name;
    }

    public static MyUser fromJson(String json){
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                    .create().fromJson(json, MyUser.class);
    }

}

How to use:

class WorkWithObjects{
    private MyUser currentUser;

    public void dataRecieved(String json){
        currentUser = MyUser.fromJson(json);
        Log.i("NAME", "IS " + currentUser.getName());
    }

}

Comments

0
private String name;
private String surname;
private int age;
private int height;
private long id;

public JSONObject writeJsonObject(){

    JSONObject obj = new JSONObject();
    try{
        obj.put("name", name);
        obj.put("surname", surname);
        obj.put("age", age);
        obj.put("height",height);
        obj.put("id", id);
    }
    catch (JSONException a){
        a.printStackTrace();
    }
    return obj ;
}
public Test(JSONObject obj) {
    this.name = obj.getString("name");
    this.surname = obj.getString("surname");
    this.age = obj.getInt("age");
    this.height = obj.getInt("height");
    this.id = obj.getLong("id");
}

Comments

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.