2016-05-26 1 views
-1

私はキーと値のセットを持つハッシュマップを持っています。HashMap - > Json - >印刷 - ファイルを作成しない - > fasterxml APIを使用して

json形式に変換して文字列全体を印刷したいと考えています。

私はファイルを作成したくないので、画面に文字列を動的に印刷する必要があります。私はfasterxml APIを使用しています。 (http://wiki.fasterxml.com/JacksonInFiveMinutes

私にそれを行う方法を教えてください。

答えて

0

以下をご覧ください: jackson-core-2.7.4.jarjackson-databind-2.7.4.jarjackson-annotations-2.7.4.jar:私は瓶、次の使用している

import java.util.HashMap; 
import java.util.Map; 

import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 

public class Test { 

public static void main(String[] args) throws JsonProcessingException { 
    Map<String, Object> map = new HashMap<String, Object>(); 
    map.put("language", "Java"); 
    map.put("year", 2016); 
    map.put("isObjectOriented", true);   
    ObjectMapper mapper = new ObjectMapper(); 
    String jsonInString = mapper.writeValueAsString(map);  
    System.out.printf("JSON: %s", jsonInString);  
} 

} 

。それが役に立てば幸い。

+0

@StanleyRこの回答は問題を解決しますか? –

関連する問題