我正在使用 Jackson API 将我的 JSON 响应映射到 java 对象。 例如,
对于响应 { name :'karthikeyan',age:'24',gender:'Male'}
@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;
是 Mix-in 并且工作正常。(在内部我们将映射此 pojo 和 Mix-in)。现在我如何在 Mix-in 中表示以下响应?
{
name :'karthikeyan',
age:'24',
gender:'Male',
interest:
{
books:'xxx',
music:'yyy',
movie:'zzz'
}
}
我试过以下方法,但没有成功。
@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;
@JsonProperty("interest")
public InterestPojo interestPojo; //created same format mix-in and pojo for interest params as well.
但无法准确映射它们,请给出您的意见和想法?
最佳答案
我尝试了以下方法:
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new Nested("books", "Music", "Movie"))));
public class Something {
@JsonProperty("name")
public String name;
@JsonProperty("age")
public int age;
@JsonProperty("gender")
public String gender;
@JsonProperty("interest")
public Nested nested;
//Constructor
}
public class Nested {
@JsonProperty("books")
public String books;
@JsonProperty("music")
public String music;
@JsonProperty("movie")
public String movie;
//Constructor
}
输出是:
{
"name":"Name",
"age":12,
"gender":"male",
"interest":
{
"books":"books",
"music":"Music",
"movie":"Movie"
}
}
所以一切都按预期工作。我已经检查过如果您提供一些 setter 和 getter 并将字段的可见性设置为私有(private)是否有区别,但这并没有什么不同。
也许您想向我们展示您的 InterestPojo 或您的输出/堆栈跟踪?
编辑: 好的,我想我明白了 ;)
我尝试了以下方法:
public void start() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Nested.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Nested.class);
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new NestedImpl("name", null))));
}
public final class Something {
private final String name;
private int age;
private String gender;
// thats your interest thing
public Nested nested;
public Something(String name, int age, String gender, Nested nested) {
this.name = name;
this.age = age;
this.gender = gender;
this.nested = nested;
}
String getName() {
return name;
}
Nested getNested() {
return nested;
}
}
public abstract class Nested {
@JsonProperty("name-ext")
abstract String getName();
@JsonProperty("interest-ext")
abstract Nested getNested();
}
public class NestedImpl extends Nested {
private String name;
private Nested nested;
private NestedImpl(String name, Nested nested) {
this.name = name;
this.nested = nested;
}
@Override
String getName() {
return name;
}
@Override
Nested getNested() {
return nested;
}
}
输出:
{
"age":12,
"gender":"male",
"name-ext":"Name",
"interest-ext":
{
"name-ext":"name",
"interest-ext":null
}
}
这不完全是您的结构,但我认为这就是您想要的。我说得对吗?
EDIT2:我用 JSON->Object 和 Object->JSON 测试了以下结构。
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getSerializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
Nested nested = new Nested();
nested.setName("Nested");
nested.setNumber(12);
Something some = new Something();
some.setName("Something");
some.setAge(24);
some.setGender("Male");
some.setNested(nested);
String json = mapper.writeValueAsString(some);
System.out.println(json);
Something some2 = mapper.readValue(json, Something.class);
System.out.println("Object: " + some2);
public abstract class Mixin {
@JsonProperty("name")
private String _name;
@JsonProperty("age")
private int _age;
@JsonProperty("gender")
private String _gender;
@JsonProperty("interest")
private Nested nested;
}
public class Something {
private String _name;
private int _age;
private String _gender;
private Nested nested;
// You have to provide Setters and Getters!!
}
public abstract class NestedMixin {
@JsonProperty("nameNested")
private String name;
@JsonProperty("numberNested")
private int number;
}
public class Nested {
private String name;
private int number;
// You have to provide Setters and Getters!!
}
输出:
{"age":24,"gender":"Male","name":"Something","interest":{"nameNested":"Nested","numberNested":12}}
对象:Something{name=Something, age=24, gender=Male, nested=Nested{name=Nested, number=12}}
注意:jackson 似乎在内部类方面遇到了问题。因此,如果您在额外的项目中测试该示例,则会创建额外的类文件;)
EDIT3:如果您使用的是模块,请尝试以下操作:
public class JacksonMixinModule extends SimpleModule {
public JacksonMixinModule() {
super("JacksonMixinModule", new Version(0, 1, 0, "SNAPSHOT"));
}
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(Something.class, Mixin.class);
context.setMixInAnnotations(Nested.class, NestedMixin.class);
}
}
...
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonMixinModule());
关于java - 如何为嵌套的 JSON 响应映射 Mixins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751615/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?