【问题标题】:Google Json arraylist returning 0 valueGoogle Json 数组列表返回 0 值
【发布时间】:2016-03-06 12:47:13
【问题描述】:

我的 Google Json arrylist 返回 0 值,但不应该。

我不想返回这个值:

this--"total":0,"records":0,"page":0,"rows":0,

下面的代码返回这个数组:

[  
  {  
    "message":"sdfghjkl",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  },
  {  
    "message":"knee problem",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  },
  {  
    "message":"hip problem",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  },
  {  
    "message":"patient message",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  },
  {  
    "message":"hip problem 2-12-2015",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  },
  {  
    "message":"hip problem 12-2015",
    "total":0,
    "records":0,
    "page":0,
    "rows":0
  }
]

代码:

Gson gson = new Gson();
List<PatientDto> all_pati_message = new ArrayList<PatientDto>();
String sql = "";
try {

  sql = "SELECT message from patient_mess";

  ResultSet rs;
  rs = DatabaseConn.getConnection().createStatement().executeQuery(sql);
  while (rs.next()) {
    PatientDto dto1 = new PatientDto();
    dto1.setMessage(rs.getString(1));
    all_pati_message.add(dto1);
  }
} catch (Exception e) {
  e.printStackTrace();
}
String jsonString = gson.toJson(all_pati_message);
return jsonString;    

【问题讨论】:

    标签: json spring-4


    【解决方案1】:
          GsonBuilder builder = new GsonBuilder();
            builder.excludeFieldsWithoutExposeAnnotation();
            final Gson gson = builder.create();
    

    并在您的 dto 类 PatientDto 中使用 google json 的注释,例如 @Expose 私有字符串消息

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多