50,499
社区成员
发帖
与我相关
我的任务
分享
java.lang.NoSuchFieldError: ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
at com.jd.open.api.sdk.internal.parser.JsonParser.<init>(JsonParser.java:24)
at com.jd.open.api.sdk.internal.parser.ParserFactory.<clinit>(ParserFactory.java:12)
at com.jd.open.api.sdk.DefaultJdClient.parse(DefaultJdClient.java:126)
at com.jd.open.api.sdk.DefaultJdClient.execute(DefaultJdClient.java:90)
package com.jd.open.api.sdk.internal.parser;
import com.jd.open.api.sdk.JdException;
import com.jd.open.api.sdk.internal.JSON.JSON;
import com.jd.open.api.sdk.internal.util.StringUtil;
import com.jd.open.api.sdk.response.AbstractResponse;
import java.util.Iterator;
import java.util.Map.Entry;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
public class JsonParser
implements Parser
{
private final ObjectMapper mapper = new ObjectMapper();
public JsonParser()
{
this.mapper.getDeserializationConfig().set(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
this.mapper.getDeserializationConfig().set(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
this.mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public <T extends AbstractResponse> T parse(String json, Class<T> responseClass)
throws JdException
{
AbstractResponse response = null;
try
{
if (StringUtil.isEmpty(json)) {
throw new JdException("response json is empty!");
}
response = fromJson(json, responseClass);
if (response != null)
response.setMsg(json);
}
catch (Exception e) {
throw new JdException(e);
}
return response;
}
public <T extends AbstractResponse> T fromJson(String json, Class<T> responseClass) throws Exception
{
ObjectNode rootNode = null;
try {
rootNode = (ObjectNode)this.mapper.readTree(json);
} catch (Exception e) {
rootNode = (ObjectNode)this.mapper.readTree(JSON.toString(JSON.parse(json)));
}
String innerJson = ((JsonNode)((Map.Entry)rootNode.getFields().next()).getValue()).toString();
return ((AbstractResponse)this.mapper.readValue(innerJson, responseClass));
}
}