alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

对象属性首字母大写 序列化之后全部变成小写了。 业务限制首字母大写怎么设置呢? #373

Closed yanyinxi closed 8 years ago

yanyinxi commented 9 years ago

public class RestResHeaderDomain {

private String method;

@JSONField(name = "Ver")
private String Ver;

public static void main(String[] args) { //com.alibaba.fastjson.util.TypeUtils.compatibleWithJavaBean = true; String str= JSON.toJSONString(new RestResHeaderDomain());

    System.out.println(str);
}

结果: {"ver":"2.0"}

不知道这个怎么用啊? com.alibaba.fastjson.util.TypeUtils.compatibleWithJavaBean = true;

@zhuzhaoyuan @pepov @stepinto @pentiumao

yanyinxi commented 9 years ago

@wenshao 我用fastjson 1.2.1

yanyinxi commented 9 years ago

试了下,在get方法上@JSONField(name = "Ver") 就可以大写。

有没有 JSON.toJSONString(restReqDomain); 这个方法里面设置的参数呢

@wenshao

LQZYC commented 9 years ago

应该还可以用namefilter来设置 或者你这样来定义bean 如下: public static class VO {

    private int Id;

    public int get_Id() {
        return Id;
    }

    public void set_Id(int id) {
        Id = id;
    }
}

结果:{"Id":123} 就是在get和set之间加一个_

wenshao commented 8 years ago

现在可以使用内置的com.alibaba.fastjson.serializer.PascalNameFilter来实现

JSON.toJSONObject(obj, new com.alibaba.fastjson.serializer.PascalNameFilter());
cbyniiaii commented 8 years ago

怎么控制全部呢?使用JSON.toJSONObject(obj, new com.alibaba.fastjson.serializer.PascalNameFilter());就变成全部大写了。能不能原来是怎么样就输出怎么样呢?