java处理Json大字符串和常用数据结构的相互转换

用到的类库:


    com.alibaba
    fastjson
    1.2.29

函数:

JSON.toJSONString,JSONObject.parseObject,JSONObject.parseArray
注意点:json转换成bean的情况,bean一定要有无参构造函数。

 

例子:

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huhuhu.utils.MapUtils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by huorong on 17/12/20.
 */
public class JsonBeanTest {
    // map -> json
    @Test
    public void mapToJson() {
        Map map = generatorMap();
        String json = JSON.toJSONString(map);
        System.out.println(json);
    }

    // json -> map
    @Test
    public void jsonToMap() {
        String json = JSON.toJSONString(generatorMap());
        Map map = JSONObject.parseObject(json);
        System.out.println(MapUtils.getStr(map, "name"));
    }

    // bean -> json
    @Test
    public void beanToJson() {
        Person person = generatorBean();
        String json = JSON.toJSONString(person);
        System.out.println(json);
    }

    // json -> bean
    @Test
    public void jsonToBean() {
        String json = JSON.toJSONString(generatorBean());
        Person p = JSONObject.parseObject(json, Person.class);
        System.out.println(p.getAge());
    }

    // List -> json
    @Test
    public void listMapToJson() {
        List mapList = generatorList();
        String json = JSON.toJSONString(mapList);
        System.out.println(json);
    }

    // json -> List
    @Test
    public void jsonToListMap() {
        String json = JSON.toJSONString(generatorList());
        List mapList = JSONObject.parseArray(json, Map.class);
        mapList.forEach(System.out::println);
    }

    // List -> json
    @Test
    public void listBeanToJson() {

        List people = generatorByListBean();
        String json = JSON.toJSONString(people);
        System.out.println(json);
    }

    // json -> List
    @Test
    public void jsonToListBean() {
        List people = generatorByListBean();
        String json = JSON.toJSONString(people);
        List personList = JSONObject.parseArray(json, Person.class);
        personList.forEach(System.out::println);
    }

    private List generatorByListBean() {
        List personList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            personList.add(new Person("huhuhuHR", (int) (Math.random() * 100)));
        }
        return personList;
    }

    private List generatorList() {
        List listMap = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            listMap.add(MapUtils.of("name", "huhuhuHR", "age", (int) (Math.random() * 100)));
        }
        return listMap;
    }

    private Person generatorBean() {
        return new Person("huhuhuHR", 18);
    }

    private Map generatorMap() {
        Map map = MapUtils.of("name", "huhuhuHR", "age", 18);
        return map;
    }

    @Test
    public void jsonToMaps() {
        String text = "{\n" + "\"file\":\"flash_lz.png\",\n" + "\"frames\":{\n" + "\"cghb_jxyx\":{\n" + "\"x\":1764,\n" + "\"y\":874,\n" + "\"w\":243,\n" + "\"h\":134,\n" + "\"offX\":0,\n" + "\"offY\":0,\n" + "\"sourceW\":243,\n" + "\"sourceH\":134\n" + "}}}";
        Map map = JSONObject.parseObject(text);
        diguiMap(map);
    }

    public void diguiMap(Map map) {
        for (Object o : map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String value = JSON.toJSONString(entry.getValue());
            System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
            if (value.startsWith("{")) {
                Map map1 = JSONObject.parseObject(value);
                diguiMap(map1);
            }
        }
    }
}

你可能感兴趣的:(java)