杰克逊错误:没有合适的构造

我使用的是杰克逊反序列化一个JSON数组一些对象。 这里是我的类:

public class OfferContainer extends ActiveRecordBase{ public Offer offer; public OfferContainer(){} public OfferContainer(Database db) { super(db); } @Override public String toString() { return this.getID() + offer.toString(); } public String getDescription() { return offer.description; } public String getBusinessName() { return offer.business.name; } public class Offer { public Category category; public String description; public String discount; public Date expiration; public Date published; public String rescinded_at; public String title; public String hook; public Date valid_from; public Date valid_to; public String id; public Business business; public Location location; public String image_270x155; public Offer() { } @Override public String toString() { return String.format( "[Offer: category=%1$s, description=%2$s, discount=%3$s, expiration=%4$s, published=%5$s, rescinded_at=%6$s, title=%7$s, valid_from=%8$s, valid_to=%9$s, id=%10$s, business=%11$s, location=%12$s]", category, description, discount, expiration, published, rescinded_at, title, valid_from, valid_to, id, business, location); } } public enum Category { Salon, Spa, Restaurant, Other } // public class Category { // public String category; // // public String toString() { // return String.format("[Category: category=%1$s]", category); // } // } public class Business { public String name; public String phone; public Address address; public Business(){} @Override public String toString() { return String.format( "[Business: name=%1$s, phone=%2$s, address=%3$s]", name, phone, address); } } public class Address { public String address_1; public String address_2; public String city; public String cross_streets; public String state; public String zip; public Address() { } @Override public String toString() { return String.format( "[Address: address_1=%1$s, address_2=%2$s, city=%3$s, cross_streets=%4$s, state=%5$s, zip=%6$s]", address_1, address_2, city, cross_streets, state, zip); } } public class Location { public double latitude; public double longitude; public Location() { } public String toString() { return String.format("[Location: longitude=%1$s, latitude=%2$s]", longitude, latitude); } } }

和错误:

W/System.err(26911): org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class org.me.pojos.OfferContainer$Business]: can not instantiate from JSON object (need to add/enable type information?) W/System.err(26911): at [Source: java.io.StringReader@405fec40; line: 1, column: 382] (through reference chain: org.me.pojos.OfferContainer["offer"]->org.me.pojos.Offer["business"]) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) W/System.err(26911): at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) W/System.err(26911): at org.codehaus.jackson.map.deser.SettableBeanProperty$FieldProperty.deserializeAndSet(SettableBeanProperty.java:579) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:635) W/System.err(26911): at org.codehaus.jackson.map.deser.SettableBeanProperty$InnerClassProperty.deserializeAndSet(SettableBeanProperty.java:780) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697) W/System.err(26911): at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) W/System.err(26911): at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:104) W/System.err(26911): at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18) W/System.err(26911): at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723) W/System.err(26911): at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854) W/System.err(26911): at org.me.OffersUpdater.updateOffersIfNeeded(OffersUpdater.java:107) W/System.err(26911): at com.activities.Main$UpdateOffersTask.doInBackground(Main.java:265) W/System.err(26911): at com.activities.Main$UpdateOffersTask.doInBackground(Main.java:239) W/System.err(26911): at android.os.AsyncTask$2.call(AsyncTask.java:185) W/System.err(26911): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) W/System.err(26911): at java.util.concurrent.FutureTask.run(FutureTask.java:138) W/System.err(26911): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) W/System.err(26911): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) W/System.err(26911): at java.lang.Thread.run(Thread.java:1019)

我一直在使用GSON但​​需要放弃它在考虑性能问题。 当我切换到杰克逊,我只是添加默认构造上所有的课,这可能是不必要的,因为有没有定义其他构造...

编辑:哦,和JSON如下:

[ { "offer" : { "business" : { "address" : { "address_1" : "340 9th Avenue", "address_2" : null, "city" : "New York", "cross_streets" : null, "state" : "NY", "zip" : "10001" }, "name" : "Blarney Stone", "phone" : "2125024656" }, "category" : "Restaurant", "claim_link" : "http://m.thinknear.com/offers/BLARNEY__1323954754?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "description" : "$1 off all drinks This discount was authorized by Toni Rossi. ", "discount" : null, "distance" : 161.40291744228713, "draws" : [ "American" ], "expiration" : "2011-12-15T21:59:59Z", "hook" : "$1 Off Drinks", "id" : "BLARNEY__1323954754", "image_270x155" : "https://s3.amazonaws.com/default_images/restaurant_stock_255x170.jpg", "location" : { "latitude" : "40.750444", "longitude" : "-73.99824579999999" }, "mobile_claim_link" : "http://m.thinknear.com/offers/BLARNEY__1323954754?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "published" : "2011-12-15T13:12:37Z", "rescinded_at" : null, "title" : "$1 Off All Drinks", "valid_from" : "2011-12-15T13:12:34Z", "valid_to" : "2011-12-15T21:29:59Z" } }, { "offer" : { "business" : { "address" : { "address_1" : "252 W 31st St", "address_2" : null, "city" : "New York", "cross_streets" : null, "state" : "NY", "zip" : "10019" }, "name" : "Hush Spa for Men", "phone" : "2127570508" }, "category" : "Spa", "claim_link" : "http://m.thinknear.com/offers/HUSH_SPA_1323962075?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "description" : "Use this offer now to enjoy this great Spa at a 30% discount. Applies to all services except massages. This discount was authorized by Andy Paningua. ", "discount" : "30", "distance" : 185.37847063528784, "draws" : [ "Body Work", "Facial", "Hair Removal" ], "expiration" : "2011-12-16T02:59:59Z", "hook" : "30% OFF", "id" : "HUSH_SPA_1323962075", "image_270x155" : "https://s3.amazonaws.com/ThinkNearMobileImages/hush_255x170.jpg", "location" : { "latitude" : "40.7499612", "longitude" : "-73.9942143" }, "mobile_claim_link" : "http://m.thinknear.com/offers/HUSH_SPA_1323962075?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "published" : "2011-12-15T15:14:36Z", "rescinded_at" : null, "title" : "30% off at Hush Spa for Men", "valid_from" : "2011-12-15T15:14:35Z", "valid_to" : "2011-12-16T02:29:59Z" } }, { "offer" : { "business" : { "address" : { "address_1" : "481 8th Ave", "address_2" : "Ste 740 ", "city" : "New York", "cross_streets" : "34th & 35th (New Yorker Hotel)", "state" : "NY", "zip" : "10001" }, "name" : "Fusion Spa", "phone" : "+18325329272" }, "category" : "Spa", "claim_link" : "http://m.thinknear.com/offers/FUSION_S_1323979416?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "description" : "Use this offer now to enjoy this great Spa at a 20% discount. New customers only. Not valid with other offers. By appointment only. Call ahead for appointment. This discount was authorized by Tiffany Albert. ", "discount" : "20", "distance" : 350.0873566571568, "draws" : [ "Facial" ], "expiration" : "2011-12-16T01:59:59Z", "hook" : "20% OFF", "id" : "FUSION_S_1323979416", "image_270x155" : "https://s3.amazonaws.com/ThinkNearMobileImages/Fusion%2BSpa+1.jpg", "location" : { "latitude" : "40.7526135", "longitude" : "-73.99334859999999" }, "mobile_claim_link" : "http://m.thinknear.com/offers/FUSION_S_1323979416?app_id=kz4hjo&latitude=40.75042&longitude=-73.99633", "published" : "2011-12-15T20:03:38Z", "rescinded_at" : null, "title" : "20% off at Fusion Spa", "valid_from" : "2011-12-15T20:03:36Z", "valid_to" : "2011-12-16T01:29:59Z" } } ]

--------------解决方案-------------

我还没有与杰克逊的工作,但我猜的问题是业务类是成员类不是静态的。

杰克逊什么需要做的是

new OfferContainer.Business()

它不能做到这一点,因为它是一个成员的类。 尝试使类的静态。

public static class Business{
....
}

我想接受,因为它使我在正确的方向前,有人贴出答案。 我做的每一个成员类的静态(后来只好@JsonIgnoreProperties(ignoreUnknown = TRUE)添加到其中之一)和它的作品。

分类:java的 时间:2012-01-01 人气:0
本文关键词: Java中,杰克逊
分享到:

相关文章

  • 在Java中的HTTP请求的Json? 2012-01-13

    如何使Java中的HTTP请求的Json? 任何图书馆? 在"HTTP JSON请求"我的意思是让POST和JSON对象的数据,并免费获赠结果为JSON. --------------解决方案------------- 除了 ​​做HTTP请求本身-这可以通过使用java.net.URL.openConnection完成哪怕只是-你只需要一个JSON库. 为便于结合/从POJO的,我会建议杰克逊. 所以,是这样的: // First open URL connection (using

  • 如何从Java中的URL获取JSON数据? 2012-08-10

    我已签出许多页,但大部分的教程和脚本返回错误代码这种类型的JSON的输出. 那么,如何将能够从这个JSON在Java中提取数据?: [ { "user":{"id":"1","username":"user1"}, "item_name":"item1", "custom_field":"custom1" }, { "

  • 解析JSON记录提取关键和价值,在java中投入地图 2012-10-06

    我在我的表一列,将数据存储在字符串格式样本数据 {"pre-date":{"enable":true,"days":"3","interval":"1","mail-template":"582"},"on-date":{"enabled":false},"post-date":{&quo

  • 如何HTTP请求正文转换成Java中的JSON对象 2012-10-24

    我试图找到一个Java的lib / API,让我把一个HTTP请求POST主体的内容成为一个JSON对象. 理想情况下,我想使用Apache的吊索库(因为它们是在我的容器暴露自然). 我发现它最接近的: org.apache.sling.commons.json.http其转换头JSON. HTTP POST机构都在格式key1=value1&key2=value2&..&keyn=valueN ,所以我认为有东西在那里,但我还没有能够找到它. 我可能只需要使用一个自定义JSONT

  • 如何在Java中创建JSON结构? 2012-11-29

    var abc = {"action":"Remove", "datatable":[ {"userid":"userid0","username":"name0"}, {"userid":"userid1","username":"name1"}, {"userid"

  • PatternSyntaxException:非法使用重复Java中的正则表达式时, 2013-03-02

    我不知道很多正则表达式,但我需要匹配一个简单的模式. 以下应返回true, Pattern.matches("{\"user_id\" : [0-9]*}", inputLine) 当inputLine是 {"user_id" : 34} 不过,我得到这个异​​常: java.util.regex.PatternSyntaxException: Illegal repetition {"user_id" : 24} at ja

  • 是否有任何MongoDB的映射在Java中? 像MongoMapper和Mongoid在Rails的? 2013-08-28

    我用Java开发与MongoDB的. 是否有任何MongoDB的映射在Java中? 像MongoMapper和Mongoid在Rails的? --------------解决方案------------- 还有吗啡 检查出吗啡. Trye bson4jackson,它采用全功能杰克逊的数据映射器从BSON绑定到任何Java对象. 杰克逊本身并JSON-向/从POJO的结合; bson4jackson增加能力为读/写BSON代替JSON.

  • java的杰克逊JSON处理器 - 使用RestTemplate Android和类型的自动转换 2013-12-13

    我必须用Java编写的代理客户端连接到JSON的WebService. 我有WebService的方法和类型只有文字说明. 例如,一个方法的结果是 PARAMS {字节[]挑战; 字节[] PROFF; } 如果我在Java中创建类PARAMS与两个领域的byte [],杰克逊映射器将它们视为二进制数组和编码为下面的例子 {"id":2,"method":"Authenticate","params":["bSwY+

  • 是否有可能反序列化JSON Java中的树? 2014-02-21

    我从Facebook,这是我不希望反序列化到一个自定义的Java对象JSON响应. 主要是因为没有保证,他们的API将保持稳定. 一旦他们改变我的反序列化将会失败是肯定的. 我想是他们的JSON数据反序列化为HashMap<String, Object> ,其中Object可能是一个String或一个HashMap . 在PHP中,它被称为关联数组,它是由生产json_decode()函数. 是否有可能做同样在Java中? --------------解决方案------------- 当然.

  • JSON模式验证器和转换在Java中 2014-03-08

    我有两个要求: 验证org.json.JSONObject针对某个架构(http://tools.ietf.org/html/draft-zyp-json-schema-03) 转换一个org.json.JSONObject或它的字符串表示到另一个org.json.JSONObject . 例如,转换 {"字段1":"值1","字段2":"VALUE2"} 至 {"FIELD3":"值1"

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 1.056 (s). 10 q(s)