java 没有很多类的 GSON 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16595493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:24:30  来源:igfitidea点击:

GSON parsing without a lot of classes

javajsonparsinggson

提问by Reeebuuk

I have the following JSON and I'm only interested in getting the elements "status", "lat"and "lng".

我有以下 JSON,我只对获取元素感兴趣"status""lat"并且"lng".

Using Gson, is it possible to parse this JSON to get those values without creating the whole classes structure representing the JSON content?

使用Gson,是否可以解析此 JSON 以获取这些值,而无需创建表示 JSON 内容的整个类结构?

JSON:

JSON:

{
  "result": {
    "geometry": {
      "location": {
        "lat": 45.80355369999999,
        "lng": 15.9363229
      }
    }
  },
  "status": "OK"
}

回答by Perception

You don't need to define any new classes, you can simply use the JSON objects that come with the Gson library. Heres a simple example:

您无需定义任何新类,只需使用 Gson 库附带的 JSON 对象即可。这是一个简单的例子:

JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonObject locObj = rootObj.getAsJsonObject("result")
    .getAsJsonObject("geometry").getAsJsonObject("location");

String status = rootObj.get("status").getAsString();
String lat = locObj.get("lat").getAsString();
String lng = locObj.get("lng").getAsString();

System.out.printf("Status: %s, Latitude: %s, Longitude: %s\n", status,
        lat, lng);

Plain and simple. If you find yourself repeating the same code over and over, then you can create classes to simplify the mapping and eliminate repetition.

干净利落。如果您发现自己一遍又一遍地重复相同的代码,那么您可以创建类来简化映射并消除重复。

回答by MikO

It is indeed possible, but you have to create a custom deserializer. See Gson documentation hereand Gson API Javadoc herefor further info. And also take a look at other reponses of mine hereand here... and if you still have doubts, comment.

这确实是可能的,但是您必须创建一个自定义的 deserializer。见GSON文档这里和GSON API的Javadoc在这里作进一步的信息。还可以在这里这里看看我的其他回复……如果您仍然有疑问,请发表评论。

That said, in my opinion it is much easier for you to parse it creating the correspondent classes, even more taking into account the simplicity of your JSON response... With the usual approach you only have to write some super-simple classes, however, writing a custom deserializer, although is not that complex, it will take you probably longer, and it will be more difficult to adapt if later on you need some data else of your JSON...

也就是说,在我看来,解析它创建相应的类要容易得多,更要考虑到您的 JSON 响应的简单性......使用通常的方法,您只需要编写一些超级简单的类,但是,编写自定义反序列化器,虽然不是那么复杂,但它可能会花费你更长的时间,如果以后你需要一些其他的 JSON 数据,它会更难以适应......

Gson has a way of operating that has been designed for developers to use it, not for trying to find workarounds!

Gson 有一种操作方式,专为开发人员使用而设计,而不是试图寻找解决方法!

Anyway, why do you not want to use classes? If you don't like to have many classes in your project, you can just use nested classes and your project will look cleaner...

无论如何,你为什么不想使用类?如果你不喜欢在你的项目中有很多类,你可以只使用嵌套类,你的项目看起来会更干净......