0

我必须完成另一个程序员的项目,他从杰克逊注释和 REST apis 开始。我没有这方面的经验,现在挣扎了几个小时。我需要像这样解析 json 数组:

{
...
"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" 
          }
      ]
}  

现有的类是这样的:

@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;

...
}

我在这里阅读了如何解析数组,但是如何使用 Jackson 注释获取jsonResponseString ?我想念什么?

谢谢!

4

2 回答 2

2

除了遗漏了许多有助于回答您的问题的东西之外,我猜您不确定 JSON 数组如何映射到 Java 对象。如果是这样,它应该是直截了当的:您将 JSON 数组映射为 Java 数组或Collections(如Lists):

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 作为对象读取:

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

我希望这有帮助。

于 2013-08-19T23:51:17.967 回答
0

我以前从未使用过它,但这里似乎帮助了另一个用户:如何使用 Jackson 注释序列化这个 JSON?

另外,如果可以的话,为什么不直接使用 Android 默认 JSON 类呢?谷歌搜索了一个随机教程:http ://www.androidhive.info/2012/01/android-json-parsing-tutorial/

于 2013-08-15T20:57:57.313 回答