`

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

阅读更多
前端JSON:
JavaScript中的JSON
http://www.dreamdu.com/blog/2008/10/19/json_in_javascript/
用 javascript 处理 JSON
http://jelly.iteye.com/blog/138707



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

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

相关推荐

    The JavaScript Object Notation (JSON) Data Interchange Format.pdf

    The JavaScript Object Notation (JSON) Data Interchange Format,RFC 8259,

    前台的转换

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the ...

    superobject-master.zip

    - JSON (JavaScript Object Notation) is a lightweight data-interchange format. - It is easy for humans to read and write. - It is easy for machines to parse and generate. - It is based on a subset of ...

    superxmlparser xml json 解析

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the ...

    C++ API for the JSON object interchange format

    CAJUN* is a C++ API for the JSON object interchange format. JSON is like XML, except it doesn't suck**. It is specifically designed for representing (in plain text format) structures familiar to ...

    json.jar(org.java.jar) 20200518

    JSON is a light-weight, language independent, data interchange format. See http://www.JSON.org/ The files in this package implement JSON encoders/decoders in Java. It also includes the capability to ...

    delphi superobject

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the ...

    json-lib-1.0.jar

    JSON in Java [package org.json] Douglas Crockford douglas@crockford.com 2011-02-02 JSON is a light-weight, language independent, data interchange format. See Java使用json时用到的jar包

    JSON格式规范

    language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of ...

    JavaScript JSON Cookbook(PACKT,2015)

    JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It's widely used today by common web applications, as ...

    Beginning.JSON.1484202031

    Chapters 3 through 12 will uncover what data is, how to convert that data into a transmittable/storable format, how to use AJAX to send and receive JSON, and, lastly, how to reassemble that data back...

    JSON键值对序列化和反序列化解析

    什么是JSON? JSON (JavaScript Object Notation) is a lightweight data-interchange ...翻译:Json【javascript对象表示方法】,它是一个轻量级的数据交换格式,我们可以很简单的来读取和写它,并且它很容易被计算机

    C#中实现Json序列化与反序列化的几种方式

    什么是JSON? JSON (JavaScript Object Notation) is a lightweight data-interchange ...翻译:Json【javascript对象表示方法】,它是一个轻量级的数据交换格式,我们可以很简单的来读取和写它,并且它很容易被计算机

    JavaScript.JSON.Cookbook.1785286900

    JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It's widely used today by common web applications, as ...

    IOS JSON库

    JSON (JavaScript Object Notation) is a light-weight data interchange format that's easy to read and write for humans and computers alike. This framework implements a strict JSON parser and generator ...

    PHP实现的json类实例

    本文实例讲述了PHP实现的json类。分享给大家供大家参考。具体如下: 这里注意json_encode只有(PHP 5 &gt;= 5.2.0, ...* JSON (JavaScript Object Notation) is a lightweight data-interchange * format. It is easy for h

Global site tag (gtag.js) - Google Analytics