杰克逊注解,REST和JSON解析阵 [英] Jackson annotations, REST and json array parsing
问题描述
我要完成另一个程序员,在那里他开始与杰克逊注释项目和REST API。我与它没有任何经验,挣扎小时了。我需要解析JSON数组是这样的:
I have to finish project of another programmer, where he started with Jackson annotations and REST apis. I have no experience with it, and struggling for hours for now. I need to parse json array like this:
{
...
"id_s": "3011",
"no_s": "Suteki",
"fl": [
{
"v": "1",
"m": "0",
"id_fs": "243",
"f_c": "2013-08-09 14:43:54",
id_tf": "3",
"u_c": "Aaa",
_u_c": "1347678779",
"c": "Carlos Rojas",
"c_c": "1"
}
]
}
现有的类是这样的:
Existing class is like:
@EBean
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item implements Serializable, MapMarker {
private static final long serialVersionUID = 1L;
@JsonProperty("id_s")
protected int id;
@JsonProperty("id_sucursal")
public void setId_sucursal(int id_sucursal) {
this.id = id_sucursal;
}
@JsonProperty("id_fb")
protected String idFacebook;
@JsonProperty("no_s")
private String name;
...
}
我读过<一个href=\"http://stackoverflow.com/questions/14913478/nested-json-parsing-for-android-with-jackson\">here如何解析数组,但我怎么弄的 jsonResponseString 使用杰克逊的注解?我怎么错过?
I've read here how to parse array, but how do I get jsonResponseString using Jackson annotations? What do I miss?
谢谢!
推荐答案
除了缺少很多东西,这将有助于回答你的问题,我猜,你是不知道如何JSON阵列将映射到Java对象。如果是这样,它应该是直接的:你映射JSON数组作为任何Java数组或收藏
S(如列表
S):
Aside from missing lots of things that would help answer your question, I am guessing that you are not sure how JSON Arrays would map to Java objects. If so, it should be straight-forward: you map JSON Arrays as either Java arrays, or Collection
s (like List
s):
public class Item { // i'll skip getters/setters; can add if you like them
public String id_s;
public String no_s;
public List<Entry> fl;
}
public class Entry {
public String v; // or maybe it's supposed to be 'int'? Can use that
public String m;
public int id_fs; // odd that it's a String in JSON; but can convert
public String f_c; // could be java.util.Date, but Format non-standard (fixable)
// and so on.
}
和你要么阅读JSON作为对象:
and you either read JSON as objects:
ObjectMapper mapper = new ObjectMapper();
Item requestedItem = mapper.readValue(inputStream, Item.class); // or from String, URL etc
// or, write to a stream
OutputStream out = ...;
Item result = ...;
mapper.writeValue(out, result);
// or convert to a String
String jsonAsString = mapper.writeValueAsString(result);
// note, however, that converting to String, then outputting is less efficient
我希望这有助于。
I hope this helps.
这篇关于杰克逊注解,REST和JSON解析阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!