这是我手机里数据库的数据。我想把这里的数据全部变为JSON格式的,有没有什么具体的思路,怎么操作?网上看了很多但还是很懵逼
//转换json
public String toJson(SensorInfo bean) {
String jsonresult = "";//定义返回字符串
try {
JSONObject jsonObj = new JSONObject();//构建jsonObject对象
jsonObj.put("uuid",bean.getUuid());//传入uuid和type
jsonObj.put("type",bean.getType());
JSONArray jsonArray=new JSONArray();//构建jsonarray对象
JSONObject jsonObj2=new JSONObject();
jsonObj2.put("x", bean.getX());
jsonObj2.put("y", bean.getY());
jsonObj2.put("z", bean.getZ());
jsonArray.put(jsonObj2);
jsonObj.put("data",jsonArray);
jsonresult = jsonObj.toString();//生成返回字符串
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("生成的json串为:",jsonresult);
return jsonresult;
}
你只有查出来结果之后把结果封装成json对象啊 怎么能把json形式的直接存在数据库里面
json对象肯定是放不进数据库的,可以放json格式字符串,但是没有必要啊,json格式是键值,对应了数据库的列行,这样存{id:1}不好吧
数据库的数据查询出来以后用写个方法把它转成json格式就好了