本文介绍了如何将Vector转换为JSONArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将 Vector< Vector< Float>> 转换为 JSONArray 。除了遍历vector并创建 JSONArray 之外,有没有更简单的方法可以做到这一点?
有人告诉我我试试gson。
解决方案SharedPreferences 只是一个键值商店。什么阻止你完全绕过 JSONObject ,而只是使用类似的东西(仅限Gson)?
private static final Type DATA_TYPE = New TypeToken< Vector< Vector< Float>>>(){} .getType();存储:
矢量<矢量<浮法>> data = new Vector< Vector< Float>>(); data.add(new Vector< Float>()); data.get(0).add(3.0f); String dataAsJson = new Gson()。toJson(data,DATA_TYPE); sharedPreferences.edit()。putString(data,dataAsJson)mit();检索:
String dataAsJson = sharedPreferences.getString(data,[]); Vector< Vector< Float>> data = new Gson()。fromJson(dataAsJson,DATA_TYPE);声明:我从未为Android开发过。
I need to convert a Vector<Vector<Float>> to a JSONArray. Apart from iterating through the vector and creating the JSONArray, is there any simpler way to do this?
Someone told me to try gson.
解决方案SharedPreferences is just a key-value store. What's stopping you from bypassing JSONObject completely, and just using something like this (Gson only)?
private static final Type DATA_TYPE = new TypeToken<Vector<Vector<Float>>>() {}.getType();Storage:
Vector<Vector<Float>> data = new Vector<Vector<Float>>(); data.add(new Vector<Float>()); data.get(0).add(3.0f); String dataAsJson = new Gson().toJson(data, DATA_TYPE); sharedPreferences.edit().putString("data", dataAsJson)mit();Retrieval:
String dataAsJson = sharedPreferences.getString("data", "[]"); Vector<Vector<Float>> data = new Gson().fromJson(dataAsJson, DATA_TYPE);Disclaimer: I've never developed for Android.
更多推荐
如何将Vector转换为JSONArray?
发布评论