2010-11-27 2 views
0

Ich habe schon einige Leute gesehen, die Probleme mit der GSON-Bibliothek hatten - ich konnte immer noch keine Lösung für mein Problem finden.Parsen von Twitter Public Timeline (JSON) in Java-Objekte über GSON

Was ich versuche zu erreichen, ist JSON von Twitter's public timeline in ein POJO mit GSON zu parsen.

Das folgende ist meine Vorstellung von JSON Struktur twitter:

public class OneTweet { 


public OneTweet() 
{ 
    // Empty constructor 
} 


public String coordinates; 
public String favorited; 
public String created_at; 
public String truncated; 
public String text; 
public String contributors; 
public String id; 
public String geo; 
public String in_reply_to_user_id; 
public String place; 
public String in_reply_to_screen_name; 
//public user user; 

public List<user> user; 


public String source; 
public String something; 


} 

public class user { 

public String name; 
public String profile_sidebar_border_color; 
public String profile_background_tile; 
public String profile_sidebar_fill_color; 
public String created_at; 
public String profile_image_url; 
public String location; 
public String profile_link_color; 
public String follow_request_sent; 
public String url; 
public String favourites_count; 
public String contributors_enabled; 
public String utc_offset; 
public String id; 
public String profile_use_background_image; 
public String profile_text_color; 

// protected is a reserved identifier 
public String pprotected; 

public String followers_count; 
public String lang; 
public String notifications; 
public String time_zone; 
public String verified; 
public String profile_background_color; 
public String geo_enabled; 
public String description; 
public String friends_count; 
public String statuses_count; 
public String profile_background_image_url; 
public String following; 
public String in_reply_to_status_id; 
public String screen_name; 

} 

Map<String, OneTweet> json_string_array = googlejson.fromJson(jsonstring, new TypeToken<Map<String, OneTweet>>(){}.getType()); 

ich zur Zeit folgende Fehlermeldung:

Exception in thread "main" com.google.gson.JsonParseException: Expecting object found: false

Ich vermute, entweder meine POJO-Struktur ist falsch oder vielleicht hat es damit zu tun, dass das JSON-Objekt "protected" ein geschützter Bezeichner in Java ist. Wenn das der Fall ist, würde ich nicht wissen, wie ich es beheben kann.

+1

Was es ausgegeben hat, wenn Sie den Anruf in 'versuchen wickeln {...} catch (Exception e) {e.printStackTrace(); } '? – mckamey

Antwort

4

Warum das Rad neu erfinden? Sie können twitter4j verwenden. Es führt das Parsen durch und gibt Ihnen die benötigten Objekte.

+0

versuchte es - und funktioniert! – jcfrei

2

Alle Felder sind nicht String. zB: Benachrichtigung ist boolean

import com.google.gson.annotations.SerializedName; 

    public class Tweets { 

@SerializedName("geo") 
public String geo; 

@SerializedName("in_reply_to_status_id") 
public String in_reply_to_status_id; 

@SerializedName("truncated") 
public String truncated; 

@SerializedName("created_at") 
public String created_at; 

@SerializedName("retweet_count") 
public String retweet_count; 

@SerializedName("in_reply_to_user_id") 
public String in_reply_to_user_id; 

@SerializedName("id_str") 
public String id_str; 

@SerializedName("place") 
public String place; 

@SerializedName("favorited") 
public boolean favorited; 

@SerializedName("source") 
public String source; 

@SerializedName("in_reply_to_screen_name") 
public String in_reply_to_screen_name; 

@SerializedName("in_reply_to_status_id_str") 
public String in_reply_to_status_id_str; 

@SerializedName("id") 
public long id; 

@SerializedName("contributors") 
public String contributors; 

@SerializedName("coordinates") 
public String coordinates; 

@SerializedName("retweeted") 
public boolean retweeted; 

@SerializedName("text") 
public String text; 

@SerializedName("profile_image_url") 
public String profile_image_url; 

// public user user; 
public User user; 


} 

und

import com.google.gson.annotations.SerializedName; 

public class User { 

@SerializedName("friends_count") 
public int friends_count; 

@SerializedName("profile_background_color") 
public String profile_background_color; 

@SerializedName("profile_background_image_url") 
public String profile_background_image_url; 

@SerializedName("created_at") 
public String created_at; 

@SerializedName("description") 
public String description; 

@SerializedName("favourites_count") 
public int favourites_count; 

@SerializedName("lang") 
public String lang; 

@SerializedName("notifications") 
public boolean notifications; 

@SerializedName("id_str") 
public String id_str; 

@SerializedName("default_profile_image") 
public boolean default_profile_image; 

@SerializedName("profile_text_color") 
public String profile_text_color; 

@SerializedName("default_profile") 
public boolean default_profile; 

@SerializedName("show_all_inline_media") 
public boolean show_all_inline_media; 

@SerializedName("contributors_enabled") 
public boolean contributors_enabled; 

@SerializedName("geo_enabled") 
public boolean geo_enabled; 

@SerializedName("screen_name") 
public String screen_name; 

@SerializedName("profile_sidebar_fill_color") 
public String profile_sidebar_fill_color; 

@SerializedName("profile_image_url") 
public String profile_image_url; 

@SerializedName("profile_background_tile") 
public boolean profile_background_tile; 

@SerializedName("follow_request_sent") 
public boolean follow_request_sent; 

@SerializedName("url") 
public String url; 

@SerializedName("statuses_count") 
public int statuses_count; 

@SerializedName("following") 
public boolean following; 

@SerializedName("time_zone") 
public String time_zone; 

@SerializedName("profile_link_color") 
public String profile_link_color; 

@SerializedName("protected") 
public boolean protectedd; 

@SerializedName("verified") 
public boolean verified; 

@SerializedName("profile_sidebar_border_color") 
public String profile_sidebar_border_color; 

@SerializedName("followers_count") 
public int followers_count; 

@SerializedName("location") 
public String location; 

@SerializedName("name") 
public String name; 

@SerializedName("is_translator") 
public boolean is_translator; 

@SerializedName("id") 
public long id; 

@SerializedName("listed_count") 
public int listed_count; 

@SerializedName("profile_use_background_image") 
public boolean profile_use_background_image; 

@SerializedName("utc_offset") 
public int utc_offset; 

} 
Verwandte Themen