How to parse a JSON string into JsonNode in Jackson?

前端 未结 6 1485
星月不相逢
星月不相逢 2020-11-29 15:55

It should be so simple, but I just cannot find it after being trying for an hour #embarrasing.

I need to get a JSON string, for example, {\"k1\":v1,\"k2\":v2}<

6条回答
  •  抹茶落季
    2020-11-29 16:01

    New approach to old question. A solution that works from java 9+

    ObjectNode agencyNode = new ObjectMapper().valueToTree(Map.of("key", "value"));
    

    is more readable and maintainable for complex objects. Ej

    Map agencyMap = Map.of(
            "name", "Agencia Prueba",
            "phone1", "1198788373",
            "address", "Larrea 45 e/ calligaris y paris",
            "number", 267,
            "enable", true,
            "location", Map.of("id", 54),
            "responsible", Set.of(Map.of("id", 405)),
            "sellers", List.of(Map.of("id", 605))
    );
    ObjectNode agencyNode = new ObjectMapper().valueToTree(agencyMap);
    

提交回复
热议问题