数据交换格式(data-interchange format) JSON

[b][size=large]前端JSON:[/size][/b]
JavaScript中的JSON
[url]http://www.dreamdu.com/blog/2008/10/19/json_in_javascript/[/url]
用 javascript 处理 JSON
[url]http://jelly.iteye.com/blog/138707[/url]


[b][size=large]后台Java JSON:[/size][/b]
java json处理框架,目前比较好的(性能等考量)两个是jackson和gson。比较:
[url]http://stackoverflow.com/questions/2378402/jackson-vs-gson[/url]在json-tools、jackson、gson等中,jackson的性能无疑是最好的,着重抓住它的API就行了。

[b][size=medium]JACKSON:[/size][/b]
1 jackson Serialization:
[url]http://stackoverflow.com/questions/5430524/serialization-of-key-value-pairs-in-jackson[/url]
2 Using jackson deserialize a jsonString to java nested object:
[url]http://stackoverflow.com/questions/8921089/jackson-converting-json-property-to-nested-object-with-dot-notation[/url]
3 jsonString中的key,在对应的java entity中使用 @JsonProperty 注解 做对应。
4 对 non-static inner class,jackson 可以将其 serializable 序列化,但不可以 deserializable 反序列化一个 json 字符串给该 non-static inner class,详见:
[url]http://jira.codehaus.org/browse/JACKSON-594[/url]
[url]http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html[/url]
[url]http://stackoverflow.com/questions/7144912/why-is-a-serializable-inner-class-not-serializable[/url]
5 @JsonCreator 。。。。。。。
老版本的jackson只能通过默认的无参构造方法
新的支持通过 @JsonCreator 来用有参构造方法来做反序列化(当同时没有无参构造方法被定义时尤其有用),并且可以基于其对 enum 做反序列化:
。。。。。。。。
[url]http://wiki.fasterxml.com/JacksonFeatureCreators[/url]
[url]http://stackoverflow.com/questions/11838039/jackson-3rd-party-class-with-no-default-constructor[/url]
[url]http://stackoverflow.com/questions/9300191/how-to-annotate-enum-fields-for-deserialization-using-jackson-json?rq=1[/url]
[url]http://stackoverflow.com/questions/8790389/jackson-deserialize-one-base-enums[/url][quote]例子:需要反序列化的类 CacheKey 的结构如下:

@JsonIgnoreProperties(ignoreUnknown=true)
public class CacheKey implements Serializable {

private String key;
private CacheType cacheType;

public CacheKey(String key, CacheType cacheType) {
Validate.notEmpty(key, "key of CacheKey must be not empty");
Validate.notNull(cacheType, "cacheType of CacheKey must be not null");
this.key = key;
this.cacheType = cacheType;
}
}

public enum CacheType {

CACHE_TYPE_A("cache-type-a", 60*60*4), // caching 4 hours
CACHE_TYPE_A("cache-type-b", 60*60*24); // caching 1 day

private final String prefix; // prefix of cacheType's key
private int exp; // default expire time in seconds

private CacheType(String prefix, int exp) {
this.prefix = prefix;
this.exp = exp;
}
}
方式一:Factory-based Creator(在类 CacheKey 中添加如下静态工厂方法并注解为@JsonCreator):

public class CacheKey implements Serializable {
@JsonCreator
public static CacheKey fromValue(@JsonProperty("key") String key, @JsonProperty("type") String type) {

CacheType cacheType = null;
for (CacheType c: CacheType.values()) {
if (c.getPrefix().equals(type)) {
cacheType = c;
}
}

if (null == cacheType) {
throw new IllegalArgumentException("Invalid type of cache: " + type);
}

return new CacheKey(key, cacheType);
}
}
方式二:Constructor-based Creator:

public class CacheKey implements Serializable {

@JsonCreator
public CacheKey(@JsonProperty("key") String key, @JsonProperty("type") CacheType cacheType) {
Validate.notEmpty(key, "key of CacheKey must be not empty");
Validate.notNull(cacheType, "cacheType of CacheKey must be not null");
this.key = key;
this.cacheType = cacheType;
}
}

public enum CacheType {
@JsonCreator
public static CacheType fromPrefix(String prefix) {
for (CacheType c: CacheType.values()) {
if (c.getPrefix().equals(prefix)) {
return c;
}
}
throw new IllegalArgumentException("Invalid CacheType: [" + prefix +
"]. Acceptable CacheType can be " +
Arrays.asList(CacheType.values()));
}
}
[/quote]6 一个ObjectMapper的使用例子:
ObjectMapper  mapper=  new ObjectMapper();
GetJSONData getJons = new GetJSONData();
String json = getJons.getJsonString(urlPath);
String jsonString = json.substring(json.indexOf("\"data\":") + 7,
json.length() - 1);
List<RequestsCount> someClassList =
mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, RequestsCount.class));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值