How to convert Java String to JSON Object

后端 未结 5 1268
暖寄归人
暖寄归人 2020-12-29 12:07

This question has been asked earlier, but I am unable to figure out the error in my code from the responses to those questions.


I am trying to convert a java s

5条回答
  •  别那么骄傲
    2020-12-29 12:26

    Converting the String to JsonNode using ObjectMapper object :

    ObjectMapper mapper = new ObjectMapper();
    
    // For text string
    JsonNode = mapper.readValue(mapper.writeValueAsString("Text-string"), JsonNode.class)
    
    // For Array String
    JsonNode = mapper.readValue("[\"Text-Array\"]"), JsonNode.class)
    
    // For Json String 
    String json = "{\"id\" : \"1\"}";
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonParser jsonParser = factory.createParser(json);
    JsonNode node = mapper.readTree(jsonParser);
    

提交回复
热议问题