How to convert jsonString to JSONObject in Java

前端 未结 19 3008
一生所求
一生所求 2020-11-22 00:39

I have String variable called jsonString:

{\"phonetype\":\"N95\",\"cat\":\"WP\"}

Now I want to convert it into JSON Object. I

19条回答
  •  情深已故
    2020-11-22 01:21

    String to JSON using Jackson with com.fasterxml.jackson.databind:

    Assuming your json-string represents as this: jsonString = {"phonetype":"N95","cat":"WP"}

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    /**
     * Simple code exmpl
     */
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(jsonString);
    String phoneType = node.get("phonetype").asText();
    String cat = node.get("cat").asText();
    

提交回复
热议问题