在Android中將對象轉換為JSON

[英]Convert object to JSON in Android


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

Android中是否有簡單的方法將任何對象轉換為JSON ?

6 个解决方案

#1


191  

Most people are using gson: https://github.com/google/gson

大多數人都在使用gson: https://github.com/google/gson

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

#2


50  

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 "";
    }

}

#3


7  

Might be better choice:

可能是更好的選擇:

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

#4


2  

Spring for Android do this using RestTemplate easily:

Android的Spring很容易使用RestTemplate實現這一點:

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);

#5


1  

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

到Android 3.0 (API級別11)的時候,Android有了一個更新和改進的JSON解析器。

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

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.

讀取JSON (RFC 4627)編碼值作為令牌流。這個流包含文本值(字符串、數字、布爾值和null)以及對象和數組的開始和結束分隔符。令牌以深度優先順序遍歷,與它們在JSON文檔中出現的順序相同。在JSON對象中,名稱/值對由一個標記表示。

#6


-2  

This how you can convert a JSON string to JSON Object natively without using any external libraries and get corresponding values for a key. Hopefully the below should work.

這就是如何在不使用任何外部庫的情況下將JSON字符串轉換為JSON對象,並為鍵獲取相應的值。希望下面的內容可以發揮作用。

String JSONstring = "{/"key1/":/"I am Value for Key1/"}";

//Import these libraries
import org.json.JSONException;
import org.json.JSONObject;

//Code below
try
{
String myVariable = jsonObject.optString("key1", "Fallback Value if key1 is not present");
System.out.println("Testing: "+ myVariable);
}
catch (JSONException e) {e.printStackTrace();}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/04/06/7121b907d3715c7bc736e9fb70caa4e6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com