将Json解析为Java对象 [英] Parse Json into Java Object

查看:100
本文介绍了将Json解析为Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Json的经验很少,我不得不将复杂的Json解析为Java对象.

I've very less experience with Json and I've to parse a complex Json to Java Objects.

我已经尝试了几种方法,但都没有成功...我正在以Json格式获取我所在城市的天气预报,我需要将Json数据解析为Java Objects.

I've tried several approaches without success... I'm getting a weather forecast for my city in Json format and I need to parse that Json data into Java Objects.

Json:

{"city":
 {"city_code":"ATAT10678",
  "name":"Wien",
  "url":"oesterreich/wien/ATAT10678.html",
  "credit":{"info":"In order to use the free weather data from wetter.com you HAVE TO display at least two out of three of the following possibilities: text, link, logo",
  "text":"Powered by wetter.com","link":"http://www.wetter.com",
  "logo":"Download at http://www.wetter.com/api/downloads/#logos"},
  "forecast":{
   "2014-08-24":{
    "w":"1",
    "tx":"20",
    "pc":"30",
    "06:00":{
     "w":"2",
     "tx":"16",
     "pc":"30",
     "tn":"15",
     "p":"5",
     "dhl":"2014-08-24 06:00",
     "ws":"19",
     "w_txt":"wolkig"},
    "11:00":{
     "w":"2",
     "tx":"18",
     "pc":"30",
     "tn":"16",
     "p":"6",
     "dhl":"2014-08-24 11:00",
     "ws":"20",
     "w_txt":"wolkig"},
    "17:00":{
     "w":"1",
     "tx":"20",
     "pc":"20",
     "tn":"16",
     "p":"6",
     "dhl":"2014-08-24 17:00",
     "ws":"12",
     "w_txt":"leicht bewölkt"},
    "23:00":{
     "w":"1",
     "tx":"16",
     "pc":"10",
     "tn":"13",
     "p":"6",
     "dhl":"2014-08-24 23:00",
     "ws":"7",
     "w_txt":"leicht bewölkt"},
    "tn":"15",
    "p":"24",
    "dhl":"2014-08-24 06:00",
    "ws":"14",
    "w_txt":"leicht bewölkt"},
   "2014-08-25":{"w":"2","tx":"22","pc":"30","06:00":{"w":"2","tx":"17","pc":"20","tn":"12","p":"5","dhl":"2014-08-25 06:00","ws":"5","w_txt":"wolkig"},"11:00":{"w":"2","tx":"21","pc":"30","tn":"17","p":"6","dhl":"2014-08-25 11:00","ws":"10","w_txt":"wolkig"},"17:00":{"w":"2","tx":"22","pc":"30","tn":"18","p":"6","dhl":"2014-08-25 17:00","ws":"11","w_txt":"wolkig"},"23:00":{"w":"3","tx":"18","pc":"30","tn":"16","p":"6","dhl":"2014-08-25 23:00","ws":"6","w_txt":"bedeckt"},"tn":"12","p":"24","dhl":"2014-08-25 06:00","ws":"8","w_txt":"wolkig"},"2014-08-26":{"w":"3","tx":"22","pc":"75","06:00":{"w":"3","tx":"17","pc":"75","tn":"15","p":"5","dhl":"2014-08-26 06:00","ws":"6","w_txt":"bedeckt"},"11:00":{"w":"61","tx":"21","pc":"75","tn":"17","p":"6","dhl":"2014-08-26 11:00","ws":"9","w_txt":"leichter Regen"},"17:00":{"w":"61","tx":"22","pc":"75","tn":"18","p":"6","dhl":"2014-08-26 17:00","ws":"9","w_txt":"leichter Regen"},"23:00":{"w":"3","tx":"18","pc":"75","tn":"17","p":"6","dhl":"2014-08-26 23:00","ws":"9","w_txt":"bedeckt"},"tn":"15","p":"24","dhl":"2014-08-26 06:00","ws":"8","w_txt":"bedeckt"}}}}

我不知道如何将其解析为对象.

I've no idea how I can parse this to objects..

非常感谢您的建议!

这是我的第一次审判.

        Gson gson = new Gson();
        JsonObject jsonObj = gson.fromJson(br, JsonObject.class);
        Map<String, LinkedTreeMap> map = new HashMap<String, LinkedTreeMap>();
        map = (Map<String, LinkedTreeMap>) gson.fromJson(jsonObj.toString(), map.getClass());

        LinkedTreeMap<String, LinkedTreeMap> tmp = new LinkedTreeMap<>();
        tmp = map.get("city");

        for(Map.Entry<String, LinkedTreeMap> e : tmp.entrySet()) {
            System.out.println("k: " + e.getKey());
        }

        LinkedTreeMap<String, LinkedTreeMap> tmp1 = new LinkedTreeMap<>();
        tmp1 = tmp.get("forecast");

        for(Map.Entry<String, LinkedTreeMap> e : tmp1.entrySet()) {
            System.out.println("k: " + e.getKey());
            LinkedTreeMap<String, LinkedTreeMap> values = e.getValue();
            for(Map.Entry<String, LinkedTreeMap> v : values.entrySet()) {

                System.out.println("k: " + v.getKey() + " v: " + v.getValue());
            }

        }

和一天的输出:

k: city_code
k: name
k: url
k: credit
k: forecast
k: 2014-08-25
k: w v: 2
k: tx v: 23
k: pc v: 90
k: 06:00 v: {w=2, tx=17, pc=20, tn=13, p=5, dhl=2014-08-25 06:00, ws=5, w_txt=wolkig}
k: 11:00 v: {w=2, tx=21, pc=20, tn=17, p=6, dhl=2014-08-25 11:00, ws=9, w_txt=wolkig}
k: 17:00 v: {w=2, tx=23, pc=30, tn=17, p=6, dhl=2014-08-25 17:00, ws=11, w_txt=wolkig}
k: 23:00 v: {w=3, tx=17, pc=90, tn=16, p=6, dhl=2014-08-25 23:00, ws=6, w_txt=bedeckt}
k: tn v: 13
k: p v: 24
k: dhl v: 2014-08-25 06:00
k: ws v: 8
k: w_txt v: wolkig

到目前为止还不错,但是我如何获得06:00、11:00、17:00和23:00(之所以通用,是因为可以更改时间),因为这是我需要的信息?

so far so good but how I get the 06:00, 11:00, 17:00 and 23:00 (generic because the time can be changed) because this are the informations I need?

非常感谢,BR 台风

推荐答案

以下是使用GSON的示例

Here is an example using GSON

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class CodeChefTest1 {
    public static String json = "{\"balance\": 1000.21, \"num\":100, \"is_vip\":true, \"name\":\"foo\"}";
    public static void main(String[] args) {
        JsonElement ele = new JsonParser().parse(json);
        for(java.util.Map.Entry<String, JsonElement> entr : ele.getAsJsonObject().entrySet()){
            System.out.println(entr.getKey());
            System.out.println(entr.getValue());
        }
    }
}

您可以使用上面的代码,并通过检查JSONElements(例如isJSONArray(),isJSONObject(),isJSONPrimitive()等)来循环运行它,并使用相同的策略执行适当的重新解析.

You can use the above and run it in a loop using checks on the JSONElements like isJSONArray(),isJSONObject(),isJSONPrimitive() etc and perform suitable re-parsing using the same strategy.

以上只是遍历json字符串并打印所有键值对.您可以对json字符串的日期部分执行相同的操作.

The above just iterates over the json string and prints all the key value pairs. You can do the same for the date part of your json string.

这篇关于将Json解析为Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆