just test Gson

just test Gson

code

package com.qilin.test;

import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by luoziyihao on 6/12/16.
 */
public class TestGson {
    public static final Log log = LogFactory.getLog(TestGson.class);

    class User{
        int age;
        String name;
        List<String> urlList;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getUrlList() {
            return urlList;
        }

        public void setUrlList(List<String> urlList) {
            this.urlList = urlList;
        }

        @Override
        public String toString() {
            return "User{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    ", urlList=" + urlList +
                    '}';
        }
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("age", 1);
        jsonObject.put("name", "tina");
        JSONArray jsonArray = new JSONArray();
        jsonArray.put("pitcture1");
        jsonArray.put("pitcture2");
        jsonObject.put("urlList", jsonArray);
        User user = new Gson().fromJson(jsonObject.toString(),  User.class);
        log.info(user);
    }

}

console

2016-06-12 11:59:59,110 INFO [com.qilin.test.TestGson] - <User{age=1, name='tina', urlList=[pitcture1, pitcture2]}>

posted @ 2016-06-12 11:56  骡子1号  阅读(110)  评论(0编辑  收藏  举报