フィールドシールド不要な注釈を使用してJSON-libに

定義されたノート

/**
 * 必须声明在jvm中保留(RetentionPolicy.RUNTIME)
 * @author Administrator
 *
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonIgore {

}

テストカテゴリ

public class TestJson {
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}

	@JsonIgore
	public int getAge() {
		return age;
	}

	@Test
	public void test1(){
		TestJson model = new TestJson();
		model.age = 30;
		model.name = "zq";
		
		JsonConfig config = new JsonConfig();
		config.addIgnoreFieldAnnotation(JsonIgore.class);
		
		List<TestJson> list = new ArrayList<TestJson>();
		list.add(model);
		
		//正确使用
		JSONArray right = JSONArray.fromObject(list,config);
		System.out.println(right.toString());		
		
		//错误使用
		JSONObject json = new JSONObject();
		json.put("list", list);
		System.out.println(json.toString());		
		
		//如果已经放在json中,再设置过滤则无效
		JSONObject wrong = JSONObject.fromObject(json,config);
		System.out.println(wrong.toString());
	}
}


公開された38元の記事 ウォンの賞賛4 ビュー19万+

おすすめ

転載: blog.csdn.net/tomatozq/article/details/21242483