Converting your Object to json is very easy by using JSONObject or other json libraries. Here is an example of using the JSONObject to convert a model class (Student) into json below
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.example.objecttojsondemo; import org.json.JSONException; import org.json.JSONObject; public class Student { private int id; private String gender; private int age; private String firstName; private String lastName; private String address; public Student(int id, String gender, int age, String firstName, String lastName, String address) { this.id = id; this.gender = gender; this.age = age; this.firstName = firstName; this.lastName = lastName; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } |
Here we have a basic model with properties such as id, name and so on. If we were to convert this to json we can create a method toJSON to convert this Object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public String toJSON(){ JSONObject jsonObject= new JSONObject(); try { jsonObject.put("id", getId()); jsonObject.put("gender", getGender()); jsonObject.put("age", getAge()); jsonObject.put("first_name", getFirstName()); jsonObject.put("last_name", getLastName()); jsonObject.put("address", getAddress()); return jsonObject.toString(); } catch (JSONException e) { e.printStackTrace(); return ""; } } |
Now to test we can simply create a student model and log or toast the converted json on the device.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Creates a Student Student firstStudent = new Student(1, "Male", 22, "Pane", "Detroit", "No 2 sw lane detroit"); String convertedJson = firstStudent.toJSON(); Log.d("Converted Json >>> ", convertedJson); Toast.makeText(this, convertedJson, Toast.LENGTH_SHORT).show(); } } |
Output
Converted Json >>>: {"id":1,"gender":"Male","age":22,"first_name":"Pane","last_name":"Detroit","address":"No 2 sw lane detroit"}
That’s it! Happy Coding.