我正在使用Gson来创建和解析JSON,但我遇到了一个问题.在我的代码中,我使用此字段:
@Expose
private ArrayList<Person> persons = new ArrayList<Person>();
Run Code Online (Sandbox Code Playgroud)
但我的JSON格式如下:
persons:{count:"n", data:[...]}
Run Code Online (Sandbox Code Playgroud)
数据是一系列人.
有没有办法使用Gson将此JSON转换为我的类?我可以使用JsonDeserializer吗?
我正在将我的项目从grails 2.2迁移到2.3,除了jaxrs插件之外,一切正常
我的BuildConfig.groovy看起来像:
...
grails.project.dependency.resolver = "maven"
grails.project.dependency.resolution = {
plugins {
compile ':jaxrs:0.8'
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
| Error Resolve error obtaining dependencies: The following artifacts could not be resolved: org.restlet.gae:org.restlet.ext.json:jar:2.0.0, org.restlet.gae:org.restlet:jar:2.0.0, org.restlet.gae:org.restlet.ext.servlet:jar:2.0.0: Could not find artifact org.restlet.gae:org.restlet.ext.json:jar:2.0.0 in grailsCentral (http://repo.grails.org/grails/plugins) (Use --stacktrace to see the full trace)
| Error Resolve error obtaining dependencies: The following artifacts could not be resolved: org.restlet.gae:org.restlet.ext.json:jar:2.0.0, org.restlet.gae:org.restlet:jar:2.0.0, org.restlet.gae:org.restlet.ext.servlet:jar:2.0.0: Could not find artifact org.restlet.gae:org.restlet.ext.json:jar:2.0.0 in grailsCentral (http://repo.grails.org/grails/plugins) (Use --stacktrace to see the full trace)
| …Run Code Online (Sandbox Code Playgroud) 给出以下JSON:
{
"authors": [{
"name": "Stephen King",
"books": [{
"title": "Carrie"
}, {
"title": "The Shining"
}, {
"title": "Christine"
}, {
"title": "Pet Sematary"
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
而这个对象结构:
public class Author {
private List<Book> books;
private String name;
}
public class Book {
private transient Author author;
private String title;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法,使用谷歌Java库Gson,反序列化JSON,书籍对象有引用"父"作者对象?
是否可以不使用自定义解串器?
我尝试了以下代码,用于匹配List中的单个ComplexObject
assertThat(complexObjectList, Matchers.<ComplexObject>hasItems(
hasProperty("lang", equalTo(lang)),
hasProperty("name", equalTo(name)),
hasProperty("desc", equalTo(desc)));
Run Code Online (Sandbox Code Playgroud)
我想要一个过滤器
match(lang) && match(name) && match(desc)
但是用上面的代码,我明白了
match(lang) || match(name) || match(desc)
如何验证这三种不同的hasProperty匹配器?
我有这个对象
@Validateable
class Foo {
Map<String, String> items
static constraints = {
items minSize: 1
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个测试失败了:
@Test
void shouldNotValidateIfItemsIsEmpty() {
Foo foo = new Foo(items: [:])
assert !foo.validate()
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?它应该根据grails'minSize'文档工作:"设置集合或数字属性的最小大小."
我有一个URL字段,可以是所有类型的网址.
是否有一个Zend框架验证类可以帮助我像Zend_Validate_EmailAddress那样为电子邮件做什么?
谢谢
我想将当前日期转换为美国/蒙特利尔时区.我是这样做的:
Date date = new Date();
TimeZone timeZone = TimeZone.getTimeZone ("America/Montreal");
Calendar cal = new GregorianCalendar(timeZone);
cal.setTime(date);
String whatIWant = "" + cal.get(Calendar.HOUR_OF_DAY) + ':'+
cal.get(Calendar.MINUTE)+ ':'+ cal.get(Calendar.SECOND);
log.info(whatIWant);
Run Code Online (Sandbox Code Playgroud)
转换很好,但我想知道这段代码有多强大.没有夏令时会发生什么?