循环遍历类的所有成员

发布于 2025-01-11 05:02:35 字数 1068 浏览 1 评论 0原文

在顶点中,如果我想循环“内部”类的所有成员&根据它创建一个映射(成员名称到其值)。 我该怎么做?

提前致谢。

public class fromJSON{
  

公共布尔成功; 公共整数时间戳; //1646140623 公共字符串基础; //欧元 公共字符串日期J; //2022-03-01 公共地图<字符串,十进制>费率; 公共 cls_rates 费率;

 class cls_rates {
     public Double AED; 
     public Double AFN; 
     public Double ALL; 
     public Double AMD; 
     public Double ANG; 
     public Double AOA; 
     public Double ARS; 
     public Double AUD; 
     public Double AWG; 
     public Double AZN; 
     public Double BAM; 
     public Double BBD; 
     public Double BDT; 
     public Double BGN; 
     public Double BHD; 
     public Double BIF; 
     public Double BMD; 
     public Double BND; 
     public Double BOB; 
     public Double BRL; 
     public Double BSD; 
     public Double BTC; 
     public Double BTN; 
     public Double BWP; 
     public Double BYN; 
     public Double BYR; 
     public Double BZD; 
     public Double CAD; 
     public Double CDF; 
     public Double CHF; 
     public Double CLF; 
}

}

In apex, if I want to loop over all members of a "inner" class & create a map from it (member name to its value).
How do I do that?

Thanks in advance.

public class fromJSON{
  

public boolean success;
public Integer timestamp; //1646140623
public String base; //EUR
public String dateJ; //2022-03-01
public Map<String, Decimal> rates;
public cls_rates rates;

 class cls_rates {
     public Double AED; 
     public Double AFN; 
     public Double ALL; 
     public Double AMD; 
     public Double ANG; 
     public Double AOA; 
     public Double ARS; 
     public Double AUD; 
     public Double AWG; 
     public Double AZN; 
     public Double BAM; 
     public Double BBD; 
     public Double BDT; 
     public Double BGN; 
     public Double BHD; 
     public Double BIF; 
     public Double BMD; 
     public Double BND; 
     public Double BOB; 
     public Double BRL; 
     public Double BSD; 
     public Double BTC; 
     public Double BTN; 
     public Double BWP; 
     public Double BYN; 
     public Double BYR; 
     public Double BZD; 
     public Double CAD; 
     public Double CDF; 
     public Double CHF; 
     public Double CLF; 
}

}

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

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

发布评论

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

评论(1

卸妝后依然美 2025-01-18 05:02:35

我们可能需要序列化内部类对象,并且需要使用deserializeUntyped方法将其反序列化,以将内部类成员变量保存到映射中。

//Create a instance of the inner class
fromJSON.cls_rates rates=new fromJSON.cls_rates();

//Setting some variable for demo purpose
rates.AED = 10.0;

//Serializing the inner class instance to json string 
String jsonStr = JSON.serialize(rates);


//Deserializing the json string to a map with key and values from the inner class
Map<String, Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);

System.debug('jsonMap : '+jsonMap);

We may need to serialize the inner class object and we would need to deserialize it using deserializeUntyped method for holding the inner class members variables into a map.

//Create a instance of the inner class
fromJSON.cls_rates rates=new fromJSON.cls_rates();

//Setting some variable for demo purpose
rates.AED = 10.0;

//Serializing the inner class instance to json string 
String jsonStr = JSON.serialize(rates);


//Deserializing the json string to a map with key and values from the inner class
Map<String, Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jsonStr);

System.debug('jsonMap : '+jsonMap);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文