Json Array作为对android客户端

时间:2015-05-21 06:32:06

标签: java json gson

我有一个要求,其中json数组响应应该如下所示

[{" rank":231," title":"猎人的夜晚"},{" rank&#34 ;:232,"标题":"加勒比海盗:黑珍珠的诅咒"},{" rank":233," title& #34;:" Lagaan:从前在印度"},{"排名":234,"标题":"城堡在天空& #34;}]

这个响应它由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);
    }

}

2 个答案:

答案 0 :(得分:1)

要使用 Gson ,您可以这样做:

  1. 为您的电影创建一个类:

    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;
        }
    }
    
  2. 列出您的电影列表:

    List<Movie> movies = new ArrayList<Movie>();
    
  3. 在列表中添加一些电影:

    movies.add(new Movie(1,"The Shawshank Redemption"));
    movies.add(new Movie(2,"The Godfather"));
    
  4. 将您的列表转换为Json:

    Gson gson = new Gson();
    
    String movieJsonList = gson.toJson(movies);
    
  5. 您可以看到字符串movieJsonList将是:

    [{“rank”:1,“name”:“The Shawshank Redemption”},{“rank”:2,“name”:“The Godfather”}]

答案 1 :(得分:1)

这是你的任务。我希望它会对你有所帮助。

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);
    }