将JSON转换为java Object GSON

时间:2013-06-13 10:34:08

标签: java json gson

{
"created_at": "Thu Jun 13 09:27:27 +0000 2013",
"id": 345110157237297150,
"id_str": "345110157237297152",
"text": "@sagarzope2 good afternoon 2nd retweet",
"source": "web",
"truncated": false,
"in_reply_to_status_id": 345109774226030600,
"in_reply_to_status_id_str": "345109774226030593",
"in_reply_to_user_id": 1512890215,
"in_reply_to_user_id_str": "1512890215",
"in_reply_to_screen_name": "sagarzope2",
"user": {
    "id": 1512890215,
    "id_str": "1512890215",
    "name": "sagar zope",
    "screen_name": "sagarzope2",
    "location": "",
    "description": "",
    "url": null,
    "entities": {
        "description": {
            "urls": []
        }
    },
    "protected": false,
    "followers_count": 0,
    "friends_count": 10,
    "listed_count": 0,
    "created_at": "Thu Jun 13 09:23:17 +0000 2013",
    "favourites_count": 0,
    "utc_offset": null,
    "time_zone": null,
    "geo_enabled": false,
    "verified": false,
    "statuses_count": 5,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "profile_background_color": "C0DEED",
    "profile_background_image_url": "",
    "profile_background_image_url_https": "",
    "profile_background_tile": false,
    "profile_image_url": "",
    "profile_image_url_https": "",
    "profile_link_color": "0084B4",
    "profile_sidebar_border_color": "C0DEED",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "default_profile": true,
    "default_profile_image": true,
    "following": false,
    "follow_request_sent": false,
    "notifications": false
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 0,
"favorite_count": 0,
"entities": {
    "hashtags": [],
    "symbols": [],
    "urls": [],
    "user_mentions": [
        {
            "screen_name": "sagarzope2",
            "name": "sagar zope",
            "id": 1512890215,
            "id_str": "1512890215",
            "indices": [
                0,
                11
            ]
        }
    ]
},
"favorited": false,
"retweeted": false,
"lang": "en"

}

任何人都可以建议我使用此JSON转换为java对象所需的类结构

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但我会尽力回答这个问题。

首先,这取决于您的要求。例如,如果您只需要几个字段,则可以直接获取它们,而无需解析json。

如果你想创建java对象,那么你只需要了解JSON的工作原理。

{} - >对象

[] - >阵列

所以,如果您有:{"foo":"bar"}

你的对象将是

public class Object {
   private String foo;
}

如果您有:{"foo":"bar","innerObj":{"fizz":"buzz"}}

你的对象将是

public class Object {
   private String foo;
   private InnerObj innerObj;
}

public class InnerObj {
   private String fizz;
   private InnerObj innerObj;
}

现在考虑一下。

了解JSON:http://json.org/

获取像Jackson或GSON这样的图书馆。