---
title: "flex json"
author: "chenchi1985"
date: "2012-06-20T08:44:10Z"
tags: []
categories: ["开源软件"]
slug: "flex-json"
draft: false
lastmod: "2012-06-20T08:45:10Z"
id: 1564386
url: "https://chenchi1985.iteye.com/blog/1564386"
---

使用flexjson将java对象转换为json字符串。

官方地址：http://flexjson.sourceforge.net/

 

**1.基本用法**

```
        List<SJia> list = SJia.find("region.id = ?", region).fetch();
        Map rtn = new HashMap();
        if (!list.isEmpty()) {
            rtn.put("sJiaList", list);
        }

        renderText(new JSONSerializer().include("sJiaList.name","sJiaList.id")
                .exclude("sJiaList.*").serialize(rtn));
```

 

在上面的代码中，最后的json结果中只包含SJia对象的id和name属性。

flexjson有以下几个使用中需要注意的地方：

- 对象的集合属性，默认是不包含在最终的json中的。
- 在JSONSerializer对象上连续调用include和exclude方法时，先调用的方法优先级较高。