我正在调用一个restful服务来获取服务器上的可用文档,我将JSON作为响应。我正在使用JSONBuilder构建JSON字符串,因此在调用此链接时
http://localhost:8080/httpConnector/Rest/Documents?Accept=application/json
我正在获取下面的JSON字符串:
String accept = getValue("Accept");
accept = "application/xml";
if ("application/xml".equals(accept)){
builder=new groovy.xml.MarkupBuilder(writer);
}else{
builder=new groovy.json.JsonBuilder();
}
builder{
results foaList.collect{
[
//Here I want to loop through the otaList to do something like that "ota.getName(), foa.getFlexiObject().getByString(ota.getName())"
result: [
name: it.getFlexiObject().getByString("name"),
version: it.getFlexiObject().getByString("version"),
author: it.getFlexiObject().getByString("author")
]
]
}
}
代码
builder.'results'() {
for(FlexiObjectAttachment foa: foaList){
for(ObjectTypeAttribute ota : otaList){
param.put(ota.getName(), foa.getFlexiObject().getByString(ota.getName()));
}
result(param);
}
}
现在我想以编程方式添加属性。因此,我必须遍历otaList来做类似的事情
for (key, value) in photoDic {
if let url = URL.init(string: value as! String){
let photo : PhotoRecord = PhotoRecord.init(name:key as! String, url:url)
self.photoRecords.append(photo)
}
}
此版本仅适用于xml respose。
答案 0 :(得分:0)
您可以尝试直接在foa
电话中结合使用ota
和collect
。
这样你最初创建的dict就会有正确的结构。
类似下面的示例
def foaList = [1, 2, 3, 4]
def otaList = ['A', 'B', 'C']
foaList.collect { foa ->
result = [name: "Name$foa", version: "v$foa", author: "Author$foa"]
otaList.each { ota -> result[ota] = "$ota$foa" }
[ result: result ]
}