You are here

FastJson outputed json node name first char is lowercase

Java object code:

  1. public String getBZ() {
  2. return BZ;
  3. }
  4. public void setBZ(String bZ) {
  5. BZ = bZ;
  6. }
  7. public String getNAME() {
  8. return NAME;
  9. }
  10. public void setNAME(String nAME) {
  11. NAME = nAME;
  12. }

After using com.alibaba.fastjson.JSON.toJSONString(), in the output json string , all nodes name's first char is lowercase:

  1. {
  2. "bZ" : "xzx",
  3. "nAME" : "xxx"
  4. }

Solution 1:

Initialize this when you create service instance or in any static class. If Spring is used, use InitializingBean to do this:

  1. TypeUtils.compatibleWithJavaBean = true;

Solution 2:
Use System.property

  1. public static boolean compatibleWithJavaBean = false;
  2.  
  3. static {
  4. try {
  5. String prop = System.getProperty("fastjson.compatibleWithJavaBean");
  6. if ("true".equals(prop)) {
  7. compatibleWithJavaBean = true;
  8. } else if ("false".equals(prop)) {
  9. compatibleWithJavaBean = false;
  10. }
  11. } catch (Throwable ex) {
  12. // skip
  13. }
  14. }
  15.  
  16. public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String> aliasMap, boolean sorted) {

Solution 2:
Use json annotation:

  1. @JSONField(name = "BZ")
  2. public String getBZ() {
  3. return BZ;
  4. }
  5. public void setBZ(String bZ) {
  6. BZ = bZ;
  7. }
  8. @JSONField(name = "NAME")
  9. public String getNAME() {
  10. return NAME;
  11. }
  12. public void setNAME(String nAME) {
  13. NAME = nAME;
  14. }

Ref: http://www.faceye.net/search/96604.html

Blog: