遍历 class 的所有成员

Looping over all members of a class

在顶点中,如果我想遍历“内部”class 的所有成员并从中创建一个映射(成员名称到它的值)。 我该怎么做?

提前致谢。

public class fromJSON{
  

public 布尔值成功; public 整数时间戳; //1646140623 public 字符串基; //欧元 public字符串日期J; //2022-03-01 public Map 率; public 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; 
}

}

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

//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);