본문 바로가기
Java

jsonarray to listmap 변환

by 무대포 개발자 2021. 1. 17.
728x90
반응형

JSONArray to listmap

  • JSONArray 를 listmap 으로 변환
  • testcase 있음.

source


package utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
public class JsonUtils {

    /**
     * jsonObject --> map 으로 변경
     * JSONObject 에 JSONArray 없어야 햠.
     * @param obj
     * @return
     */
    public static Map<String, Object> getMapFromJSONObject(JSONObject obj) {
        if (ObjectUtils.isEmpty(obj)) {
            log.error("BAD REQUEST obj : {}", obj);
            throw new IllegalArgumentException(String.format("BAD REQUEST obj %s", obj));
        }

        try {
            return new ObjectMapper().readValue(obj.toJSONString(), Map.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * json array 를 list map 으로 변경.
     *
     * @param jsonArray
     * @return 값이 있으면 list map, 없으면 list 빈 값 return
     */
    public static List<Map<String, Object>> getListMapFromJsonArray(JSONArray jsonArray) {
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("jsonArray is null.");
            throw new IllegalArgumentException("jsonArray is null");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Object jsonObject : jsonArray) {
            list.add(getMapFromJSONObject((JSONObject) jsonObject));
        }
        return list;
    }
}





// test

package utils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.hamcrest.Matchers.is;

public class JsonUtilsTest {

    private JSONObject jsonObject;

    private JSONArray jsonArray;

    @Before
    public void setUp() throws Exception {
        jsonObject = new JSONObject();
        jsonObject.put("key", "key test");
        jsonObject.put("value", "value test");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("key", "key test");
        jsonObject2.put("value", "value test");

        jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject2);
    }

    @Test
    public void getListMapFromJsonArray_테스트() throws Exception {
        List<Map<String, Object>> list = JsonUtils.getListMapFromJsonArray(jsonArray);
        Assert.assertThat(list.size(), is(2));
        Assert.assertThat(list.get(0).get("key"), is("key test"));
        Assert.assertThat(list.get(0).get("value"), is("value test"));
    }
}

댓글