json记点

jackson-all-1.9.0.jar

2.java2json
List<entity> list=null;
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(list);
使用上述即可完成java到json的转换,若想忽略entity的某些属性值,只需在属性声明时加上@JsonIgnore


3.json2java
List<Entity>list=mapper.readValue(jsonStr,List.class);
Map m=mapper.readValue(jsonStr,Map.class);
Entity en=mapper.readValue(jsonStr,Entity.class)

==================

json-lib.2.4-jdk15.jar

1.2 java类2json
自定义processor处理类,并可通过style值返回不同的参数值
public class Advert implements java.io.Serializable {  
    private static final long serialVersionUID = -886134245592090495L;  

public class AdvertProcessor implements JsonBeanProcessor {  

使用方法:
JsonConfig config=new JsonConfig();  
           config.registerJsonBeanProcessor(Advert.class, new AdvertProcessor(1));  
           List<Advert>list=null;  
           JSONArray  ja=JSONArray.fromObject(list, config);  
           Advert advert=null;  
           JSONObject jo=JSONObject.fromObject(advert, config);  

1.3 json2java
针对 {“id”:"","url":"","width":"","height":""}

定义对应java类
public class JsonPosterVO {  
使用方法:
单个上述对象  
JSONObject jo=JSONObject .fromObject(jsonStr);  
JSONObject.toBean(programJsonObject, JsonPosterVO.class);  
[java] view plaincopy
json数组  
JsonArray ja=JsonArray.fromObject(jsonStr);  
JsonPosterVO[]  arr=(JsonPosterVO[])JSONArray.toArray(ja,JsonPosterVO.class);  
  
List<JsonPosterVO>poster作为一个对象的属性  
Map<String, Class> mapClass = new HashMap<String, Class>();  
 mapClass.put("poster", JsonPosterVO.class);  
  JsonProgramVO[] array = (JsonProgramVO[])JSONArray.toArray(pArr, JsonProgramVO.class, mapClass); 

你可能感兴趣的:(json记点)