我想用Gson解析这个

时间:2011-05-21 14:51:25

标签: android arrays json parsing gson

   {"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 = ......;         Toast.makeText(getApplicationText(),Double.toString(n.getData()。get(i).getEarnings(),Toast.SHORT).show不起作用。显示0.0 我的问题有点像这样,但我没有得到他的回答:link ,这是我的班级:

import 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"}]

1 个答案:

答案 0 :(得分: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;
    }
}