Apex - 反序列化json后显示列表

时间:2014-01-09 09:18:52

标签: json salesforce apex

我想创建一个http callout来导入json并在visualforce页面中显示信息。我遇到的问题是json是嵌套的,所以我需要遍历子节点。

我使用json2apex应用程序派生了以下类:

public class CHForm {

public class FilingHistoryItem {
    public String DocumentDate;
    public String FormType;
    public String DocumentCategory;
    public String Document;
    public String DocumentDescription;
  }

public FilingHistory FilingHistory;

public class FilingHistory{
    public List<FilingHistoryItem> FilingHistoryItem;
  }


public static CHForm parse(String json) {
    return (CHForm) System.JSON.deserialize(json, CHForm.class);
  }


}

在我的控制器类中,我可以使用以下命令创建一个CHForm对象( reponseForm )并将json反序列化为 reponseForm 对象:

HttpResponse res = h.send(req);
String chFormJson = res.getBody();
responseForm  = CHForm.parse(chFormJson);

但是如何在visualforce页面中显示所有FilingHistoryItem的列表?我是否需要在控制器中创建列表对象,或者是否有一种直接从visualforce页面引用列表的方法?

1 个答案:

答案 0 :(得分:0)

我的顶点有点困惑,不应该是:

public List<FilingHistoryItem> FilingHistory;

public class FilingHistoryItem {
    public String DocumentDate;
    public String FormType;
    public String DocumentCategory;
    public String Document;
    public String DocumentDescription;
  }

然后你就可以做到:

<apex:repeat value="{!responseform.FilingHistory.}" var="item" id="theRepeat">
   <apex:outputText value="{! item.DocumentDate}" />  //etc
</apex:repeat

你现在的设置方式,我认为它可能会这样:

 <apex:repeat value="{!responseform.FilingHistory.FilingHistoryItem}" var="item" id="theRepeat">
       <apex:outputText value="{! item.DocumentDate}" />  //etc
    </apex:repeat

但是你将list属性命名为“... Item”并且不是“..Items”并且嵌套得如此深刻,但是这可能是json依赖,这有点奇怪。