我正在尝试在Gson输出中使用自定义日期格式,但.setDateFormat(DateFormat.FULL)似乎不起作用,它也一样.registerTypeAdapter(Date.class, new DateSerializer()).
这就像Gson不关心对象"Date"并以其方式打印它.
我怎么能改变呢?
谢谢
编辑:
@Entity
public class AdviceSheet {
public Date lastModif;
[...]
}
public void method {
Gson gson = new GsonBuilder().setDateFormat(DateFormat.LONG).create();
System.out.println(gson.toJson(adviceSheet);
}
Run Code Online (Sandbox Code Playgroud)
我一直用java.util.Date; setDateFormat()不起作用:(
我是JSON和REST的新手.我正在使用一个返回如下字符串的服务器:
[{"username":"Hello","email":"hello@email.com","credits":"100","twitter_username":""},{"username":"Goodbye","email":"goodbye@email.com","credits":"0","twitter_username":""}]
Run Code Online (Sandbox Code Playgroud)
我已经设法在控制台上将它们作为字符串打印出来,但现在我想将它们转换为JSON数组.到目前为止,我所拥有的代码没有返回任何错误,但我不知道将什么放入新JSON数组的构造函数中.我一直指的是一位同事发给我的一段代码,其中构造函数是新的JSONArray(响应),但他从未告诉我'响应'是什么.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.misc.BASE64Encoder;
public class NetClientGet {
public static void main(String[] args) {
try {
URL url = new URL("http://username:password@mobile.crowdedmedia.co.uk/index.php/api/users/get_users/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = "username:password";
String encoded = enc.encode(userpassword.getBytes());
conn.setRequestProperty("Authorization", "Basic " + encoded);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader …Run Code Online (Sandbox Code Playgroud)