javascript
flex与java间用json传输数据,如何在Java中使用flexjson通过@JSON注释控制序列化?
@JSON注釋用于通過JSONSerializer類在序列化過程,以排除或包括的字段。我們可以使用JSONSerializer類的serialize()方法對目標實例執行淺化序列化。
語法@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE,METHOD})
public?@interface?JSON
示例import?flexjson.JSONSerializer;
import?flexjson.JSON;
public?class?JSONAnnotationTest?{
public?static?void?main(String[]?args)?{
JSONSerializer?serializer?=?new?JSONSerializer().prettyPrint(true);
Employee?emp?=?new?Employee("Raja",?"Ramesh",?30,?"Hyderabad");
String?jsonStr?=?serializer.serialize(emp);
System.out.println(jsonStr);
}
}
//員工階層
class?Employee?{
private?String?firstName,?lastName,?address;
private?int?age;
public?Employee(String?firstName,?String?lastName,?int?age,?String?address)?{
super();
this.firstName?=?firstName;
this.lastName?=?lastName;
this.age?=?age;
this.address?=?address;
}
public?String?getFirstName()?{
return?firstName;
}???@JSON(include=false)
public?String?getLastName()?{
return?lastName;
}
public?int?getAge()?{
return?age;
}
@JSON(include=false)???public?String?getAddress()?{
return?address;
}
}
輸出結果{
"age":?30,
"class":?"Employee",
"firstName":?"Raja"
}
總結
以上是生活随笔為你收集整理的flex与java间用json传输数据,如何在Java中使用flexjson通过@JSON注释控制序列化?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php把一个数组放在另一个数组的后面,在
- 下一篇: MongoDB学习笔记(三)使用Spri