使用Gson库,如何将JSON字符串转换为自定义类JsonLog的数组列表?基本上,JsonLog是由我的Android应用程序制作的不同类型的日志实现的接口——短信日志、通话日志、数据日志——这个数组列表是所有这些日志的集合。第6行总是出错。
public static void log(File destination, JsonLog log) {
Collection<JsonLog> logs = null;
if (destination.exists()) {
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader(destination));
logs = gson.fromJson(br, ArrayList<JsonLog>.class); // line 6
// logs.add(log);
// serialize "logs" again
}
}
编译器似乎不明白我引用的是一个类型化的数组列表。我该怎么办?
你可以使用TypeToken将json字符串加载到一个自定义对象中。
logs = gson.fromJson(br, new TypeToken<List<JsonLog>>(){}.getType());
文档:
Represents a generic type T.
Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.
For example, to create a type literal for List<String>, you can create an empty anonymous inner class:
TypeToken<List<String>> list = new TypeToken<List<String>>() {};
This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.
科特林:
如果你需要在Kotlin中这样做,你可以这样做:
val myType = object : TypeToken<List<JsonLong>>() {}.type
val logs = gson.fromJson<List<JsonLong>>(br, myType)
或者你可以从其他选项中看到这个答案。
你的JSON样本是:
{
"status": "ok",
"comment": "",
"result": {
"id": 276,
"firstName": "mohamed",
"lastName": "hussien",
"players": [
"player 1",
"player 2",
"player 3",
"player 4",
"player 5"
]
}
所以如果你想在你的sharedpreferences中保存模块的数组列表,那么:
1-使用此方法将返回的数组列表转换为json格式
public static String toJson(Object jsonObject) {
return new Gson().toJson(jsonObject);
}
2-保存在共享prefreneces中
PreferencesUtils.getInstance(context).setString("players", toJson((.....ArrayList you want to convert.....)));
3-检索它在任何时候得到JsonString从共享首选项
String playersString= PreferencesUtils.getInstance(this).getString("players");
4-再次转换为数组列表
public static Object fromJson(String jsonString, Type type) {
return new Gson().fromJson(jsonString, type);
}
ArrayList<String> playersList= (ArrayList<String>) fromJson(playersString,
new TypeToken<ArrayList<String>>() {
}.getType());
如果你想解析对象的数组列表,这个解决方案也是可行的
希望对你使用Gson库有所帮助。
我不确定gson怎么样但这就是你和Jon相处的方式。样品希望一定有类似的方法使用gson
{
“玩家”:(
“玩家1”,
“玩家2”,
“玩家3”,
“球员4”,
“球员5”
]
}
===============================================
import java.io.FileReader;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JosnFileDemo {
public static void main(String[] args) throws Exception
{
String jsonfile ="fileloaction/fileName.json";
FileReader reader = null;
JSONObject jsb = null;
try {
reader = new FileReader(jsonfile);
JSONParser jsonParser = new JSONParser();
jsb = (JSONObject) jsonParser.parse(reader);
} catch (Exception e) {
throw new Exception(e);
} finally {
if (reader != null)
reader.close();
}
List<String> Players=(List<String>) jsb.get("Players");
for (String player : Players) {
System.out.println(player);
}
}
}
你可以使用TypeToken将json字符串加载到一个自定义对象中。
logs = gson.fromJson(br, new TypeToken<List<JsonLog>>(){}.getType());
文档:
Represents a generic type T.
Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.
For example, to create a type literal for List<String>, you can create an empty anonymous inner class:
TypeToken<List<String>> list = new TypeToken<List<String>>() {};
This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.
科特林:
如果你需要在Kotlin中这样做,你可以这样做:
val myType = object : TypeToken<List<JsonLong>>() {}.type
val logs = gson.fromJson<List<JsonLong>>(br, myType)
或者你可以从其他选项中看到这个答案。
科特林
data class Player(val name : String, val surname: String)
val json = [
{
"name": "name 1",
"surname": "surname 1"
},
{
"name": "name 2",
"surname": "surname 2"
},
{
"name": "name 3",
"surname": "surname 3"
}
]
val typeToken = object : TypeToken<List<Player>>() {}.type
val playerArray = Gson().fromJson<List<Player>>(json, typeToken)
OR
val playerArray = Gson().fromJson(json, Array<Player>::class.java)