json转map

第一种方式alibabaJson

2

import com.alibaba.fastjson.JSON;        

3

    String maintainer="{'0':'1878','1':'96','3':'880637','4':'837472','5':'1380','7':'638030','8':'968730','10':'917149','12':'1192455','15':'1217041','16':'10867','17':'1228656','18':'1216759','19':'397','20':'1359923','21':'1359928','22':'666282','23':'1540217','24':'1554147','25':'1581988','26':'1764429','27':'1799874','28':'1682381','31':'2712627','33':'770212','34':'3204370','35':'658355','36':'3446638','37':'1490805','38':'1'}";

4

        Map<String, Object> stringObjectMap = (Map)JSON.parse(maintainer);

5

        System.out.println(stringObjectMap.toString());

6

7

8

第二种方式:

9

  Map<String, Object> stringObjectMap = JsonUtils.readValue(maintainer);

10

11

12

JsonUtils类:

13

package com.ycb.wpc.provider.utils;

14

15

import com.fasterxml.jackson.databind.ObjectMapper;

16

import com.fasterxml.jackson.databind.PropertyNamingStrategy;

17

import com.google.common.collect.Maps;

18

import org.slf4j.Logger;

19

import org.slf4j.LoggerFactory;

20

21

import java.util.Map;

22

23

public class JsonUtils {

24

25

    private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);

26

27

    private final static ObjectMapper objectMapper = new ObjectMapper();

28

29

    public static String writeValueAsString(Object value) {

30

        try {

31

            objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

32

            return objectMapper.writeValueAsString(value);

33

        } catch (Exception e) {

34

            logger.error(e.getMessage(), e);

35

        }

36

        return null;

37

    }

38

39

    @SuppressWarnings("unchecked")

40

    public static Map<String, Object> readValue(String content) {

41

        try {

42

            return objectMapper.readValue(content, Map.class);

43

        } catch (Exception e) {

44

            logger.error(e.getMessage(), e);

45

        }

46

        return Maps.newHashMap();

47

    }

48

}

49

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注