What is the JSP equivalent to json_encode ( in PHP )?

前端 未结 4 1663
孤街浪徒
孤街浪徒 2020-12-03 18:26

I am trying to encode a JSP servlet into JSON. What\'s the equivalent in JSP to json_encode() in PHP ?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 18:45

    json_encode in php is similar to following package in java

    dependency:

    import com.fasterxml.jackson.databind.ObjectMapper;
    

    code :

    Map dataArray = {some data in map}
    ObjectMapper objMapper = new ObjectMapper();
    String jsonString = objMapper.writeValueAsString(dataArray);
    

    jsonString is if the final result like son_encode in php, which you can achieve with objectMapper class

提交回复
热议问题