Json 数组作为对 android 客户端的响应
Json Array as response to android client
我有一个要求,其中 json 数组响应应该如下所示
[{"rank":231,"title":"The Night of the Hunter"},{"rank":232,"title":"Pirates of the Caribbean: The Curse of the Black Pearl"},{"rank":233,"title":"Lagaan: Once Upon a Time in India"},{"rank":234,"title":"Castle in the Sky"}]
它由 android 客户端收到的响应进行了相应的解析,但我无法生成正确的响应结构。
我现在非常感谢你的帮助
@WebServlet("/JsonHost")
public class JsonHost extends HttpServlet {
private static final long serialVersionUID = 1L;
int rank;
int title;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// get request parameters for userID and password
String[] movie = {
"The Shawshank Redemption",
"The Godfather",
"The Godfather: Part II",
"The Dark Knight",
"Pulp Fiction",
"Schindler's List",
"12 Angry Men"..............};
int offset=0;
int limit=20;
if (request.getParameter("offset") != null)
offset = Integer.parseInt(request.getParameter("offset"));
System.out.println("offset value:" + offset);
JSONArray mJSONArray = new JSONArray(Arrays.asList(movie));
// System.out.println(gn.toJson(mJSONArray));
//out.println(gn.toJson(mJSONArray));
String m[][] = new String[20][20];
for (int rank = 0; rank < offset + limit && rank < movie.length; rank++){
for (int title = 0; title < offset+limit; title++) {
m[rank][title] = movie[title];
// System.out.println(m[rank][title]);
out.println(gn.toJson(m[rank][title]));
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
要使用 Gson,您可以这样做:
像这样为您的电影创建 class:
public class Movie {
private int rank;
private String name;
public Movie(int rank, String name) {
this.rank = rank;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getRank() {
return rank;
}
}
有一个电影列表,例如:
List<Movie> movies = new ArrayList<Movie>();
将一些电影添加到您的列表中:
movies.add(new Movie(1,"The Shawshank Redemption"));
movies.add(new Movie(2,"The Godfather"));
将您的列表转换为 Json:
Gson gson = new Gson();
String movieJsonList = gson.toJson(movies);
您可以看到字符串 movieJsonList
将是:
[{"rank":1,"name":"The Shawshank Redemption"},{"rank":2,"name":"The Godfather"}]
这是您的task.I 希望对您有所帮助!
JSONArray array=new JSONArray();
int index=0;
for(int i=231;i<235;i++){
JSONObject jsonObject=new JSONObject();
jsonObject.put("rank", i);
jsonObject.put("title", movie[index++]);
array.put(jsonObject);
}
我有一个要求,其中 json 数组响应应该如下所示
[{"rank":231,"title":"The Night of the Hunter"},{"rank":232,"title":"Pirates of the Caribbean: The Curse of the Black Pearl"},{"rank":233,"title":"Lagaan: Once Upon a Time in India"},{"rank":234,"title":"Castle in the Sky"}]
它由 android 客户端收到的响应进行了相应的解析,但我无法生成正确的响应结构。
我现在非常感谢你的帮助
@WebServlet("/JsonHost")
public class JsonHost extends HttpServlet {
private static final long serialVersionUID = 1L;
int rank;
int title;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// get request parameters for userID and password
String[] movie = {
"The Shawshank Redemption",
"The Godfather",
"The Godfather: Part II",
"The Dark Knight",
"Pulp Fiction",
"Schindler's List",
"12 Angry Men"..............};
int offset=0;
int limit=20;
if (request.getParameter("offset") != null)
offset = Integer.parseInt(request.getParameter("offset"));
System.out.println("offset value:" + offset);
JSONArray mJSONArray = new JSONArray(Arrays.asList(movie));
// System.out.println(gn.toJson(mJSONArray));
//out.println(gn.toJson(mJSONArray));
String m[][] = new String[20][20];
for (int rank = 0; rank < offset + limit && rank < movie.length; rank++){
for (int title = 0; title < offset+limit; title++) {
m[rank][title] = movie[title];
// System.out.println(m[rank][title]);
out.println(gn.toJson(m[rank][title]));
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
要使用 Gson,您可以这样做:
像这样为您的电影创建 class:
public class Movie { private int rank; private String name; public Movie(int rank, String name) { this.rank = rank; this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setRank(int rank) { this.rank = rank; } public int getRank() { return rank; } }
有一个电影列表,例如:
List<Movie> movies = new ArrayList<Movie>();
将一些电影添加到您的列表中:
movies.add(new Movie(1,"The Shawshank Redemption")); movies.add(new Movie(2,"The Godfather"));
将您的列表转换为 Json:
Gson gson = new Gson(); String movieJsonList = gson.toJson(movies);
您可以看到字符串 movieJsonList
将是:
[{"rank":1,"name":"The Shawshank Redemption"},{"rank":2,"name":"The Godfather"}]
这是您的task.I 希望对您有所帮助!
JSONArray array=new JSONArray();
int index=0;
for(int i=231;i<235;i++){
JSONObject jsonObject=new JSONObject();
jsonObject.put("rank", i);
jsonObject.put("title", movie[index++]);
array.put(jsonObject);
}