Thizzer
Thizzer

Reputation: 16663

Convert object to JSON in Android

Is there a simple method to convert any object to JSON in Android?

Upvotes: 172

Views: 195083

Answers (8)

madhu527
madhu527

Reputation: 4782

Anyway, you know this

Gson gson = new Gson();
String json = gson.toJson(yourModelClassReference);
   

You might have forget to add @Expose

enter image description here

Upvotes: 1

Rahul Mishra
Rahul Mishra

Reputation: 1559

The Kotlin way

val json = Gson().toJson(myObj)

Upvotes: 4

download the library Gradle:

implementation 'com.google.code.gson:gson:2.9.0'

To use the library in a method.

Gson gson = new Gson();

//transform a java object to json
System.out.println("json =" + gson.toJson(Object.class).toString());

//Transform a json to java object
String json = string_json;
List<Object> lstObject = gson.fromJson(json_ string, Object.class);

Upvotes: 12

James L
James L

Reputation: 16864

Most people are using gson : check this

Gson gson = new Gson();
String json = gson.toJson(myObj);

Upvotes: 340

Hesam
Hesam

Reputation: 53600

Might be better choice:

@Override
public String toString() {
    return new GsonBuilder().create().toJson(this, Producto.class);
}

Upvotes: 14

Saeed Zarinfam
Saeed Zarinfam

Reputation: 10180

Spring for Android do this using RestTemplate easily:

final String url = "http://192.168.1.50:9000/greeting";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Greeting greeting = restTemplate.getForObject(url, Greeting.class);

Upvotes: 4

ingyesid
ingyesid

Reputation: 2884

public class Producto {

int idProducto;
String nombre;
Double precio;



public Producto(int idProducto, String nombre, Double precio) {

    this.idProducto = idProducto;
    this.nombre = nombre;
    this.precio = precio;

}
public int getIdProducto() {
    return idProducto;
}
public void setIdProducto(int idProducto) {
    this.idProducto = idProducto;
}
public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public Double getPrecio() {
    return precio;
}
public void setPrecio(Double precio) {
    this.precio = precio;
}

public String toJSON(){

    JSONObject jsonObject= new JSONObject();
    try {
        jsonObject.put("id", getIdProducto());
        jsonObject.put("nombre", getNombre());
        jsonObject.put("precio", getPrecio());

        return jsonObject.toString();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }

}

Upvotes: 64

Mob
Mob

Reputation: 11096

As of Android 3.0 (API Level 11) Android has a more recent and improved JSON Parser.

http://developer.android.com/reference/android/util/JsonReader.html

Reads a JSON (RFC 4627) encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The tokens are traversed in depth-first order, the same order that they appear in the JSON document. Within JSON objects, name/value pairs are represented by a single token.

Upvotes: 2

Related Questions