map和json的toString

通常传参数有2种方式,使用map的方式,JSON的方式,这里举个例子。

public class Book {
    public String author;
    public int pages;
}

注意如下s1和s2结果是不一样的,一般我们使用的json方式,也就是s1的结果

   Book book = new Book();
        book.author="luxun";
        book.pages =90;

        String s1 = null,s2;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("author",book.author);
            jsonObject.put("pages",book.pages);
            s1=jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        Map<String,String> map = new HashMap<>();
        map.put("author",book.author);
        map.put("pages",""+book.pages);
        s2=map.toString();
        //s1  {"author":"luxun","pages":90}  
        //s2 {pages=90, author=luxun}

猜你喜欢

转载自blog.csdn.net/litefish/article/details/90753302