有没有一种在Android中将任何对象转换为JSON的简单方法?在Android中将对象转换为JSON
回答
大多数人都使用GSON:https://github.com/google/gson
Gson gson = new Gson();
String json = gson.toJson(myObj);
就Android 3.0(API等级11)的Android有一个较新的和改进的JSON解析器。
http://developer.android.com/reference/android/util/JsonReader.html
读取编码值作为标记的流的JSON(RFC 4627)。这个 流包括文字值(字符串,数字,布尔值和空值)以及对象和数组的开始和结束分隔符。 这些令牌按深度优先顺序遍历,它们与 出现在JSON文档中的顺序相同。在JSON对象中,名称/值 对由单个标记表示。
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 "";
}
}
我更喜欢这种方式,每个对象都有自己的stringify方法,谢谢你的洞察! – Bhimbim 2017-12-18 04:33:59
Spring for Android很容易地做到这一点使用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);
您不需要将MappingJackson2HttpMessageConverter添加到RestTemplate,如果Jackson Jar在类路径中,它会自动添加。 – 2016-10-11 14:14:43
这如何可以一个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();}
这不是一个问题的答案。此外jsonObject在这里是未知的。 – CoolMind 2016-04-08 13:02:18
可能是更好的选择:
@Override
public String toString() {
return new GsonBuilder().create().toJson(this, Producto.class);
}
- 1. 将JSON转换为模型对象Android
- 2. 将对象转换为json?
- 3. 将对象转换为json
- 4. 在android中使用gson将json数组转换为json对象?
- 5. 在PHP中将对象转换为JSON和JSON到对象
- 6. 将JSON对象转换为C#对象
- 7. 将jcas对象转换为json对象
- 8. 将javascript对象转换为json对象
- 9. 无法将JSON从WebApi转换为Android中的Json对象
- 10. JSON - 将对象转换为JSON数组
- 11. 将JSON对象转换为JSON数组
- 12. 将JSON对象转换为JSON阵列
- 13. 将JSON转换为Swift中的对象
- 14. 将json转换为C中的对象#
- 15. 无法转换为JSON对象android
- 16. 我想将此字符串转换为android中的json对象?
- 17. 如何将json对象转换为android ..中的字符串?
- 18. 在Android上将对象数组转换为JSON字符串
- 19. 将ExecutionResult对象转换为json
- 20. 将PDO对象转换为Javascript JSON
- 21. 如何将json对象转换为java
- 22. Codeigniter/PHP /将对象转换为JSON
- 23. 将字符串转换为JSON对象
- 24. 将JSON对象转换为Typescript类
- 25. 将对象转换为json格式 - iOS
- 26. Json_encode - 将数组转换为json对象
- 27. 将JSON自动转换为Java对象
- 28. 将Json转换为java对象
- 29. 如何将json转换为对象?
- 30. 将表数组转换为JSON对象
并且还杰克逊。 – Mob 2011-12-08 13:48:48
为什么我们没有toJson的嵌入方法?但是我们从杰森来? – 2017-11-22 10:31:22