有没有一种在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
- 2. 将对象转换为json?
- 3. 在android中使用gson将json数组转换为json对象?
- 4. 在PHP中将对象转换为JSON和JSON到对象
- 5. 将jcas对象转换为json对象
- 6. 将javascript对象转换为json对象
- 7. 无法将JSON从WebApi转换为Android中的Json对象
- 8. 将JSON对象转换为JSON数组
- 9. JSON - 将对象转换为JSON数组
- 10. 将JSON转换为Swift中的对象
- 11. 如何将json对象转换为android ..中的字符串?
- 12. 将JSON对象转换为C#列表
- 13. 将gwt共享对象转换为json
- 14. Automapper:将对象转换为JSON
- 15. 将NSData转换为JSON对象数组
- 16. 将JSON对象转换为Typescript类
- 17. 如何将JSON转换为对象?
- 18. 使用jackson将json转换为对象
- 19. 将json对象转换为jquery数组
- 20. 将PDO对象转换为Javascript JSON
- 21. 将Delphi对象转换为JSON
- 22. 如何将JSON转换为对象
- 23. 如何将i18n.map转换为json对象
- 24. 如何将json转换为对象?
- 25. Javascript:将SHA512值转换为JSON对象
- 26. 将JSON对象转换为数组?
- 27. 将CSV(嵌套对象)转换为JSON
- 28. 无法将javascript对象转换为json
- 29. Python - 将JSON对象转换为字典
- 30. 如何将NSDictionary转换为JSON对象?
并且还杰克逊。 – Mob 2011-12-08 13:48:48
为什么我们没有toJson的嵌入方法?但是我们从杰森来? – 2017-11-22 10:31:22