2011-05-21 85 views
0
{"type":"earnings","info":{"earnings":58.9,"dividends":2245.82,"gains":0,"expenses":2024.12,"shares_bought":0,"shares_bought_user_count":0,"shares_sold":0,"shares_sold_user_count":0},"created":"2011-05-20 11:59:17"} 

我使用Gson,但它不是一個數組,也不是一個簡單的JSON,我不知道如何檢索收益例如。 吐司不起作用: 通知n = ...; (),get(i).getEarnings(),Toast.SHORT).show不起作用。它顯示0.0 我的問題有點像這樣但我真的不明白他的答案:link ,這裏是我的類:我想分析這與Gson

進口的java.util.List;

public class Profile_notifications_data { 
private String type; 
private double earnings; 
private double dividends; 
private double gains; 
private double expenses; 
private int shares_bought; 
private int shares_bought_user_count; 
private int shares_sold; 
private int shares_sold_user_count; 
private String created; 
private String from_ticker; 
private String from_full_name; 
private String to_ticker; 
private int headline_id; 
private String to_full_name; 
private String message; 
private String subject; 
private String community_name; 
private int community_id; 
private String ticker; 
private String stock_info; 
private int shares; 
private double price; 
private String buyer_ticker; 
private String buyer_name; 
private String seller_ticker; 
private String seller_name; 

public Profile_notifications_data(){ 

} 

    public String getType(){ 
return type; 
    } 
    public String getCreated(){ 
return created; 
    } 
    public String getFromTicker(){ 
return from_ticker; 
    } 
    public String getFromFullName(){ 
return from_full_name; 
    } 
    public String getToTicker(){ 
return to_ticker; 
    } 
    public String getToFullName(){ 
return to_full_name; 
    } 
    public String getMessage(){ 
return message; 
    } 
    public String getSubject(){ 
return subject; 
    } 
    public String getTicker(){ 
return ticker; 
    } 
    public String getStockInfo(){ 
return stock_info; 
    } 
    public String getBuyerTicker(){ 
return buyer_ticker; 
    } 
    public String getBuyerName(){ 
return buyer_name; 
    } 
    public String getSellerTicker(){ 
return seller_ticker; 
    } 
    public String getSellerName(){ 
return seller_name; 
    } 
    public String getCommunityName(){ 
return community_name; 
    } 
    public double getEarnings(){ 
return earnings; 
    } 
    public double getDividends(){ 
return dividends; 
    } 
    public double getGains(){ 
return gains; 
    } 
    public double getExpenses(){ 
return expenses; 
    } 
    public double getPrice(){ 
return price; 
    } 
    public int getSharesBought(){ 
return shares_bought; 
    } 
    public int getSharesBoughtUserCount(){ 
return shares_bought_user_count; 
    } 
    public int getSharesSold(){ 
return shares_sold; 
    } 
    public int getSharesSoldUserCount(){ 
return shares_sold_user_count; 
    } 
    public int getCommunityId(){ 
return community_id; 
    } 
    public int getHeadlineId(){ 
return headline_id; 
    } 
    public int getShares(){ 
return shares; 
    } 

    } 

    "data":[{"type":"earnings","info{"earnings":50.1,"dividends":1108.77,"gains":15.2,"expenses":0,"shares_bought":0,"shares_bought_user_count":0,"shares_sold":40,"shares_sold_user_count":1},"created":"2011-03-23 11:27:54"},{"type":"earnings","info":{"earnings":58.9,"dividends":1043.48,"gains":15.19,"expenses":1575.09,"shares_bought":0,"shares_bought_user_count":0,"shares_sold":0,"shares_sold_user_count":0},"created":"2011-03-22 11:27:00"},{"type":"earnings","info":{"earnings":42.75,"dividends":792.26,"gains":15.19,"expenses":0,"shares_bought":0,"shares_bought_user_count":0,"shares_sold":140,"shares_sold_user_count":1},"created":"2011-03-21 11:26:41"},{"type":"earnings","info":{"earnings":61.75,"dividends":911.69,"gains":1226.04,"expenses":2318.53,"shares_bought":100,"shares_bought_user_count":1,"shares_sold":0,"shares_sold_user_count":0},"created":"2011-03-20 11:27:14"},{"type":"user_reply","info":{"from_ticker":"DELCON","from_full_name":"Ardella Conrad","to_ticker":"TSUNA","headline_id":"31577262","to_full_name":null,"message":"Thank you again!"},"created":"2011-03-19 22:42:02"},{"type":"shares_bought","info":{"from_ticker":"DELCON","full_name":"Ardella Conrad","shares":100,"price":"24.21718"},"created":"2011-03-19 17:37:32"},{"type":"user_reply","info":{"from_ticker":"DELCON","from_full_name":"Ardella Conrad","to_ticker":"TSUNA","headline_id":"31443202","to_full_name":null,"message":"Thanks"},"created":"2011-03-19 17:37:25"}] 

回答

2

這裏有一個Java類你有JSON字符串轉換成Java對象。

ProfileNotificationData profileNotificationData = new Gson().fromJson("YOUR JSON STRING", ProfileNotificationData.class); 

您可致電profileNotificationData.info.earnings;獲取收入。

public class ProfileNotificationData { 
    public String type; 
    public Info info; 
    public String created; 

    public static class Info{ 
     public double earnings; 
     public double dividends; 
     public double gains; 
     public double expenses; 
     public int shares_bought; 
     public int shares_bought_user_count; 
     public int shares_sold; 
     public int shares_sold_user_count; 
    } 
} 
+0

對不起,但它不起作用。 – Tsunaze 2011-05-21 16:20:13

+0

我修改了一下你的代碼,這是我的問題的答案,但是,讓我們說有一個單一的「信息」是一個數組,我該如何解決這個問題? – Tsunaze 2011-05-21 17:29:19

+0

如果您向我展示JSON字符串,我將能夠更好地解決您的問題。 – Raunak 2011-05-21 17:36:44