我想用 Gson 解析这个

发布于 2024-11-09 01:33:15 字数 4434 浏览 0 评论 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 = ...; 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"}]
   {"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"}

I use Gson but it's not an array nor a simple json, i don't know how to retrieve earnings for example.
The toast doesn't work :
Notification n = ...;
Toast.makeText(getApplicationText(), Double.toString(n.getData().get(i).getEarnings(), Toast.SHORT).show doesn't work .It display 0.0
My problem is a little like this but i don't really get his answer : link
, here's my class :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-11-16 01:33:15

下面是一个 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;
    }
}

Here's a Java class to convert the JSON string you have into a Java object.

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

You can access earnings by calling 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文