我正在使用JSON Jackson 2.0将数据模型写入文件,然后将其加载回应用程序.我可以成功地将对象写入字符串,但在尝试将其编组回对象时遇到以下错误消息:
找不到类型[simple type,class ImportAttributeModel]的合适构造函数:无法在[Source:user;来自JSON对象(需要添加/启用类型信息?)实例化)line:1,column:111](通过引用链:ImportTemplateModel ["modelList"])
这是我的类的代码片段:
public class ImportTemplateModel
{
private Map<Integer, AttributeModel> m_modelList;
private GraphElementType m_type;
public ImportTemplateModel()
{
}
// getters & setters
}
public class AttributeModel
{
private String m_label;
private String m_key;
private ElementDefinition m_definition;
public AttributeModel(String label, String key, ElementDefinition def)
{
m_label = label;
m_key = key;
m_definition = def;
}
// getters & setters
}
Run Code Online (Sandbox Code Playgroud)
这是我写的和读取模型的调用:
// Write model
ObjectMapper mapper = new ObjectMapper();
ImportTemplateModel itm = new ImportTemplateModel()
// set …Run Code Online (Sandbox Code Playgroud) 我response给了我null价值观,但当我运行同样的东西时fiddler,我得到了很多数据.我编写的代码非常少,并使用在线工具生成代码.我想知道我搞砸了.
Publications response = null ;
// First open URL connection (using JDK; similar with other libs)
try {
URL url = new URL(
"http://myserver:myport/Mobile/GetPublications");
HttpURLConnection connection = (HttpURLConnection)url.openConnection() ;
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST") ;
connection.setRequestProperty("Content-Type", "application/json") ;
// and other configuration if you want, timeouts etc
// then send JSON request
Publications request = new Publications(); // POJO with getters or public fields
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(connection.getOutputStream(), request);
// and read response …Run Code Online (Sandbox Code Playgroud) 如何将POJO映射到几个JSON演示文稿?
我在用杰克逊.
我想要下面的代码
@JsonIgnorePropertiesStreamA({ "value2" })
@JsonIgnorePropertiesOtherWay({ "value3" })
public class Value {
public int value;
public int value2;
public int value3;
}
Run Code Online (Sandbox Code Playgroud)
如何与杰克逊一起做?或者其他图书馆可以做什么?
我正在尝试使用Spring for Android和Jackson 2从休息呼叫中创建一个POJO.
// Set the Accept header
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","vnd.livescore_app.api.v1+json")));
requestHeaders.setContentType(new MediaType("application","json;charset=utf-8"));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
restTemplate.getMessageConverters().add( new MappingJackson2HttpMessageConverter());
// Make the HTTP GET request, marshaling the response from JSON to an array of Events
ResponseEntity<AppVersionObject> responseEntity = restTemplate.exchange("https://example.com", HttpMethod.GET, requestEntity, AppVersionObject.class);
AppVersionObject object = responseEntity.getBody();
Run Code Online (Sandbox Code Playgroud)
使用以下日志跟踪崩溃
05-13 10:55:42.656: E/AndroidRuntime(19401): FATAL EXCEPTION: Thread-1470
05-13 10:55:42.656: E/AndroidRuntime(19401): …Run Code Online (Sandbox Code Playgroud) 我想使用canDeserialize方法,因为在反序列化时,我想获取要在自定义反序列化中应用的类型类,如以下示例所示:
public T deserialize(byte[] bytes) throws SerializationException {
bolean isAccount = this.objectMapper.canDeserialize(??????).
T t = null;
if(isAccount)
t = (T)this.objectMapper.readValue(bytes,Account.class);
else
t = (T) this.objectMapper.readValue(bytes, 0, bytes.length, new TypeReference<Object>(){});
return t;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Account类具有@JsonDeserialize注释,用于自定义反序列化。
我最近开始开发rest apis并且偶然发现了这些条款 - jetty,jackson&jersey.我在普通日食时使用球衣成功开发了休息api.
我相信jackson是为了促进xml/java对象的反序列化和序列化.但它是什么 - 它只是一个jar文件的集合? - 如果是的话,如果我已经在使用jersey框架(如果我的apis处理xmls),我可以/我应该同时使用它吗?
同样,什么即Jetty - 我下载它并看到了很多文件夹和文件 - 而且它们不仅仅是罐子的集合!如果它是一个框架,我可以在球衣上使用它吗?如果是的话,它看起来很重 - 我认为处理HTML内容可能只是几个罐子.但看起来还有很多东西.
如果我的apis处理所有媒体类型,这三件事情是否可以一起使用?如果是,那么最好的方法是什么?
谢谢.
我正在尝试使用jackson api解析json,并提供以下详细信息.但是我收到了错误.请查找代码和堆栈跟踪的以下详细信息.
JSON
[{"id":1001,"cyPerReffe":1200,"distPercentage":"0"},{"id":1002,"cyPerReffe":1000,"distPercentage":"10"},{"id":1003,"cyPerReffe":800,"distPercentage":"20"},{"id":1004,"cyPerReffe":750,"distPercentage":"10"},{"id":1005,"cyPerReffe":700,"distPercentage":"10"},{"id":1006,"cyPerReffe":650,"distPercentage":"10"},{"id":1007,"cyPerReffe":600,"distPercentage":"20"},{"id":1008,"cyPerReffe":300,"distPercentage":"5"},{"id":1009,"cyPerReffe":333,"distPercentage":"7"},{"id":1010,"cyPerReffe":"32","distPercentage":"8"}]
Run Code Online (Sandbox Code Playgroud)
控制器类
ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
PublisheDataList publisheDataList = mapper.convertValue(json, PublisheDataList.class);
Run Code Online (Sandbox Code Playgroud)
以下类是我的bean类. PublisheData.java
private Long id;
private Long cyPerReffe;
private String distPercentage;
// with setter and getters
Run Code Online (Sandbox Code Playgroud)
PublisheDataList.java
List<PublisheData> publisheDataList;
//with setter and getters
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
java.lang.IllegalArgumentException: Can not deserialize instance of models.PublisheDataList out of START_ARRAY token
at [Source: N/A; line: -1, column: -1]
at org.codehaus.jackson.map.ObjectMapper._convert(ObjectMapper.java:2502)
at org.codehaus.jackson.map.ObjectMapper.convertValue(ObjectMapper.java:2468)
at controllers.RYSController.publishReferenceYield(RYSController.java:79)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$22$$anonfun$apply$62.apply(routes_routing.scala:318)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$22$$anonfun$apply$62.apply(routes_routing.scala:318)
at play.core.Router$HandlerInvoker$$anon$6$$anon$2.invocation(Router.scala:175)
at play.core.Router$Routes$$anon$1.invocation(Router.scala:355)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:50)
at play.GlobalSettings$1.call(GlobalSettings.java:63)
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:80)
at …Run Code Online (Sandbox Code Playgroud) 我有以下JSON:
"propertyName": "{"1":[{"1":"value1","2":"value2","3":false}]}"
Run Code Online (Sandbox Code Playgroud)
第一个属性是具有属性映射后数组中项目的计数.
如果我想填写一个包含这些值的类,那么使用Jackson对此进行反序列化的最佳方法是:
class MyHolder
{
name = "value1";
age = "value2";
female = false;
}
Run Code Online (Sandbox Code Playgroud)
例如.
我有一个从API返回的JSON,所以我无法更改它,问题是其中一个属性有时是一个空字符串,有时它是一个对象.
有时当我调用它时,foo是一个空字符串
{
"foo" : "",
"bar" : 0
}
Run Code Online (Sandbox Code Playgroud)
而在其他时候它是一个对象
{
"foo" : {"baz": 11},
"bar" : 2
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用杰克逊做到这一点?到目前为止,我发现的唯一方法是编写自定义解串器/串行器对.我对这种方法的问题是,这是一个深度嵌套的JSON结构,并且违规属性接近根...
这是我的控制器.
@RequestMapping(value="/updateItem.htm", method = RequestMethod.GET, headers="Accept=*/*", produces = "application/json; charset=utf-8")
public @ResponseBody ModelAndView updateItem(ModelAndView model) {
List<String> list = aspireDAO.getSingleCol("select prod_id from items order by prod_id asc");
JsonArray jArray = AspireUtil.toJsonArray(list);
model.setViewName("UpdateItem");
model.addObject("items", jArray);
System.out.println(jArray);
return model;
}
Run Code Online (Sandbox Code Playgroud)
对于spring-servlet.xml中的jackson支持
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true"/>
</bean>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
json数组在我的控制器sysout语句中很好用.当我检查页面的源时,json数组就像这样打印
'["amgc","atup","ciwy","eyqe","ijm
Run Code Online (Sandbox Code Playgroud)
可能是什么原因.如何解决它.
AADAM
jackson ×10
java ×8
json ×7
spring ×2
android ×1
gson ×1
jersey ×1
jetty ×1
spring-mvc ×1
web-services ×1