ObjectMapperクラスは、Jackson Libraryのメインクラスです。 JSON構造と一致するようにJavaオブジェクトに変換される機能を提供します。 JSonParserとJSongeneratorのインスタンスを使用して、JSONの実際の読み取り/書き込みを実装しています。
クラス宣言
以下は、org.codehaus.jackson.map.objectmapperクラスの宣言です。
Public Class ObjectMapperは、objectCodecを拡張しますバージョン
ネストされたクラス
| sn | クラスと説明 |
|---|---|
| 1 | static class objectmapper.defaultytepereSolverbuilder // TypereSolverbuilderをカスタマイズし、Type Resolution Builderを使用していわゆる「デフォルト入力」を提供します(詳細についてはEnabledEfaultTing()を参照)。 |
| 2 | static class objectmapper.defaulttyping // enabledefaultting()列挙を使用して、デフォルトの入力を使用する型(クラス)を指定します。 |
| sn | コンストラクターと説明 |
|---|---|
| 1 | ObjectMapper()//デフォルトのコンストラクターは、STDSERIALIALIZERPROVIDERをシリアルイザープロバイダーとして使用し、BeanserializeFactoryをシリアルイザーファクトリーとして使用するために必要に応じてデフォルトのJSONFactoryを構築します。 |
| 2 | ObjectMapper(JSONFactory JF)//指定されたJSONFactoryを使用して、必要なJSonParsersおよび/またはJSongeneratorsマップを構築します。 |
| 3 | ObjectMapper(JSONFactory JF、SerializerProvider SP、DeserializerProvider DP) |
| 4 | ObjectMapper(JSONFactory JF、SerializerProvider SP、DeserializerProvider DP、SerializationConfig Sconfig、DeserializationConfig dconfig) |
| 5 | ObjectMapper(SerializerFactory SF)は推奨されません。代わりに他のコンストラクトを使用してください。 Serialization Factory SetSerializeFactoryを設定できることに注意してください(org.codehaus.jackson.map.serializerfactory) |
サンプルテストクラスの基本コードは次のとおりです
/ * * @project java * @package * @file jackson.java * @version 1.0 * /public class jackson { / * * * class decruptonがここに行きます。 * * @Class Jackson * @version 1.0 */ public static jsongenerator jsongenerator = null; private static objectmapper mapper = new objectMapper(); public static void main(string [] args){学生= new Student(); Student.setisStudent(true); Student.setUid(1000); Student.setuname( "Xiao liao"); Student.setUpWd( "123"); Student.setNumber(12); map <string、desute> stumap = new hashmap <string、sustent>(); stumap.put( "1"、学生); stumap.put( "2"、学生); List <Object> stulist = new ArrayList <Object>();リスト<Student> Stulist1 = new ArrayList <Student>(); Stulist1.Add(学生);学生= new Student(); Student.setisStudent(false); Student.setuid(200); Student.setuname( "Xiao Mi"); Stulist1.Add(学生); Stulist.Add(学生); stulist.add(stucent xin "); stulist.add(" xiao er "); stulist.add(stumap); // readjson2array(); try {// writearray2json(array); // writeJson2( // writemap2json(stumap); writeentity2json(オブジェクト)はioexception {mapper.writevalue( "d://developsoft//aaadowload//testjson1/lib//aa.txt")、object) json arrayに変換) * @param object * @throws ioexception */public static void writearray2json(オブジェクトオブジェクト)throws ioexception {// writeValueはwriteObjed mappper.writevalue(new file( "d://developsoft//aaadowload//testjson1//-lib// lya.t"); mappper.writevalue(system.out、object); system.out.println( "ObjectMapper --------------"); // writeValueは、writeObject.out.println( "+mapper.writevalueasstring(object))と同じ関数を持っています); mapper.writevalue(system.out、object); System.out.println( "==>"+mapper.writevalue( "d://developsoft//testjson1//lib//aamap.txt")、object); * @description:todo(jsonはエンティティに変換) * @throws ioexception */ public static void writejson2entity()throws ioexception {system.out.println( "エンティティに変換されました--------------"); file(d://developsoft//aaadowload//lib/// fileinputStremstream = new fileinputStream(ファイル)出力// mapper.defaultsprettyprintingWriter() mapper.readvalue(json、sustent.class); system.out.println(json string to entity -------------- "); System.out.println(sustent.tostring()); string json = "[/" uid/"、/" uname/":/" xiao liao/"、/"/"、/" number/":12.0、/" isStudent/":true}、{uid/"/"/"/"/"/"/"/" mi/"、/" upwd/":null、" 0.0、/"isStudent/":linkedhashmap <string >> s = mapper.readvalue(json、list.class); link.keyset(); iterator = keerator.hasnext(); /*** jsonはオブジェクトをリストに変換します*/public static void readjson2list(){string json = "[{/" uid/":1、/" uname/":/" www/"、/" number/":234、/" upwd/":/" 456/"}、" + " {uid/":/"/"tom/"、 ":3.44,/":/"123/"}]」 list.size(); map <string> list.get(i); e){e.printstacktrace(); [{/"uid/":1、/"uname/":/"www/"、 "number/":234、 "upwd/":/"456/"}、 " +" {/"uid/学生= Mapper.ReadValue(学生); e.printstacktrace();印刷結果:
串转换成エンティティ------------- JSON2:uid = 1000、name = xiao liao、upwd = 123、number = 12.0、isstudent = truewritemap2json --------- {"2":{"uid":1000、 "uname": "Xiao liao "、" upwd ":" 123 "、" number ":12.0、" isStudent ":true}、" 1 ":{" uid ":1000、" uname ":" Xiao liao "、" upwd ":" 123 "、" number ":12.0、" isStudent ":true}} readjson2Array -------------------------> uid = 1、name = ww w、upwd = 456、number = 234.0、isstudent = false> uid = 5、name = tom、upwd = 123、number = 3.44、isStudent = falseWritemap2json ------------ {"2":{"uid":1000、 "uname": "xiao liao"、 "upwd": "123"、 "number":12.0、 "isStudent":true}、 "1":{"uid":1000、 "uname": "Xiao liao "、" upwd ":" 123 "、" number ":12.0、" isStudent ":true}}}みんな、自分で試してみてください、上記は私のテストコードでもあります