я хочу разобраться с Gson - PullRequest
0 голосов
/ 21 мая 2011
   {"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 Моя проблема немного похожа на это, но яна самом деле не получил его ответ: ссылка , вот мой класс:

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 Ответ

2 голосов
/ 21 мая 2011

Вот класс 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;
    }
}
...