本文介绍了Gson只专注于内部物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{count:xxx,important:[{..] 。}}}
我所关心的是重要的对象。有什么我可以用自定义反序列化器或任何可以让GSON反序列化那个重要的json对象的东西吗?
另外我想要反序列化对象的样子是像这样:
列表<重要> = Gson.fromJson(arg0,new TypeToken< ArrayList< Important>>(){}。getType());
解决方案
是的,您可以使用从Gson库读取它作为JsonObject。然后,拉出你需要的内部对象,并使用Gson来填充你的java类。
例子:
Reader jsonReader =
New BufferedReader(
new InputStreamReader(getIStream(myFile.json)));
JsonParser parser = new JsonParser();
JsonObject top = parser.parse(jsonReader).getAsJsonObject();
JsonElement importantEl = top.get(important);
//将重要元素解析为您创建的任何结构
Gson gson = new Gson();
列表<重要>重要的
= Gson.fromJson(importantEl,new TypeToken< ArrayList< Important>>(){}。getType());
Ok I have a json coming in that looks like this:
{count:xxx, important:[{...}]}
All I care about is the important object. Is there anything I can do with a custom deserializer or anything where I can just have GSON deserialize that important json object?
Also the way I want the deserialized object to look is like this:
List<Important> = Gson.fromJson(arg0, new TypeToken<ArrayList<Important>>(){}.getType() );
解决方案
Yes, you can use the JsonParser
class from the Gson library to read it as a JsonObject. Then, pull out the inner object you need and use Gson to fill your java class with it.
Example:
Reader jsonReader =
new BufferedReader(
new InputStreamReader(getIStream("myFile.json")));
JsonParser parser = new JsonParser();
JsonObject top = parser.parse(jsonReader).getAsJsonObject();
JsonElement importantEl = top.get("important");
//Parse the important element into whatever structure you're creating
Gson gson = new Gson();
List<Important> important
= Gson.fromJson(importantEl, new TypeToken<ArrayList<Important>>(){}.getType() );
这篇关于Gson只专注于内部物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!