Home > Java > javaTutorial > body text

Convert JSON to Map or Map to JSON using Jackson library in Java?

王林
Release: 2023-08-29 09:33:06
forward
853 people have browsed it

Convert JSON to Map or Map to JSON using Jackson library in Java?

JSON Jackson is a Java library that has very powerful data binding functions and provides a way to serialize custom java objects to JSON and deserialize JSON Back to the framework of Java objects. We can convert JSON to/from MapUse the readValue() and writeValueAsString() methods of the com.fasterxml.jackson.databind.ObjectMapper class.

JSON to mapping

Syntax

public <T> T readValue(String content, TypeReference valueTypeRef) throws IOException, JsonParseException, JsonMappingException
Copy after login

Example

import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
public class ConvertJSONToMapTest {
   public static void main(String args[]) {
      try {
        <strong> </strong>ObjectMapper mapper = new ObjectMapper();
         String jsonString = "{\"Name\":\"Raja\", \"Technology\":\"Java\"}";
         Map<String, Object> jsonMap = new HashMap<String, Object>();
         jsonMap = mapper.readValue(jsonString, new TypeReference<Map<String, String>>(){}); // converts JSON to Map
         System.out.println(jsonMap);
      } catch(IOException ie) {
         ie.printStackTrace();
      }
   }
}
Copy after login

Output

{Name=Raja, Technology=Java}
Copy after login

Map to JSON

Syntax

public String writeValueAsString(Object value) throws JsonProcessingException
Copy after login

Example

import java.io.*;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ConvertMapToJSONTest {
   public static void main(String args[]) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = "";
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("Raja", "Java Technology");
      map.put("Ravi", "Python Technology");
      map.put("Adithya", "Spark Technology");
      map.put("Vamsi", "Web Technology");
      jsonString = mapper.writeValueAsString(map); // converts Map to JSON<strong>
 </strong>     System.out.println(map);
   }
}
Copy after login

Output

{"Raja":"Java Technology", "Vamsi":"Web Technology", "Ravi":"Python Technology", "Adithya":"Spark Technology"}
Copy after login

The above is the detailed content of Convert JSON to Map or Map to JSON using Jackson library in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!