GithubからGSONをダウンロード:https://github.com/google/gson
GSONのアプリケーションには、主に2つの変換関数TojsonとFromJsonが含まれています。このオブジェクト変換を使用する前に、JSON文字列を対応するオブジェクトに正常に変換するには、オブジェクトカテゴリとそのメンバーを作成する必要があります。
クラスの例{private int answer1 = 100;プライベート文字列回答2 = "Hello World!";例(){} //デフォルトコンストラクター}json文字列にJavaオブジェクトをシリアル化します
例example1 =新しい例(); gson gson = new gson(); string json = gson.tojson(emple1);
JSONの結果はそうなるでしょう
{"Answer1":100、 "Answer2": "Hello World!"}JSONの文字列を対応するJavaオブジェクトにゆるくします
例2 = gson.fromjson(json、examples.class);
==> example2はexample1と同じです
Object Example1は、Tojsonを介してJSON文字列にシリアル化され、JSONを受け取った後、Object Example2がJSONからjsonを介してsebly2に脱必要にされることを宣言します。したがって、例1はexample2と同じです。
例:
java.util.arraylistをインポートします。 java.util.arraysをインポートします。 java.util.collectionをインポートします。 java.util.hashmapをインポートします。 java.util.mapをインポートします。 com.google.gson.gsonをインポートします。 com.google.gson.reflect.typetokenをインポートします。 class user {public user(string name、int age、stringbuffer sex、boolean ischild){this.name = name; this.age = age; this.sex = sex; this.ischild = ischild; }プライベート文字列名;プライベートインクエイジ;プライベートストリングバッファーセックス;プライベートブールイズカイル; public String toString(){return "{name ="+name+"; age ="+age+"; sex ="+sex+"; ischild ="+ischild+"}"; } public int hashcode(){return name.hashcode()*100+age; }} public class gsontest {public static void main(string [] args){gson gson = new gson(); System.out.println( "1つの普通の豆の構成*****************************"); system.out.println( "beanをjson string->"に変換する); user user1 = new user( "fengjie"、12、new StringBuffer( "nown")、true); system.out.println( "conversion"+user1);文字列json = gson.tojson(user1); system.out.println( "json string、json ==="+jsonへのユーザーオブジェクト); System.out.println( "***************************"); system.out.println( "json文字列をBean->"に変換); user user2 = gson.fromjson(json、user.class); system.out.println( "user2 =="+user2に変換); System.out.println(); System.out.println( "2collectionコレクションに変換*********************************************"); system.out.println( "BeanのリストコレクションをJSON String->>>に変換); collection <user> userlist1 = new ArrayList <user>(); for(int i = 0; i <3; i ++){user user = new user( "ruhua"、10+i、new stringbuffer( "male")、false); userlist1.add(user); } json = gson.tojson(userlist1); system.out.println( "ユーザーのリストコレクションオブジェクトは、json文字列、json ==="+jsonに変換されます); System.out.println( "***************************"); system.out.println( "json文字列を豆のリストコレクションに変換 - >"); collection <user> userlist2 = gson.fromjson(json、new typetoken <collection <user >>(){}。getType()); system.out.println( "ユーザーのリストコレクションに変換、userlist2 ="+userlist2); System.out.println(); System.out.println( "3Arrayアレイの変換***************************"); system.out.println( "beanの配列をjson string->に変換する - >"); user [] userarray1 = new user [3]; for(int i = 0; i <userArray1.length; i ++){userArray1 [i] = new user( "hidden"、20、new StringBuffer( "Shemale")、true); } json = gson.tojson(userArray1); system.out.println( "ユーザーの配列オブジェクトはjson文字列、json ==="+jsonに変換されます); System.out.println( "******************************"); system.out.println( "json文字列をBean->の配列オブジェクトに変換する); user [] userarray2 = gson.fromjson(json、new typetoken <user []>(){}。getType()); system.out.println( "ユーザーの配列オブジェクトに変換、userArray2 ="+arrays.toString(userArray2)); System.out.println(); System.out.println( "4mapを変換**************************************); system.out.println( "beanのマップをjson string->に変換する - >"); map <string、user> map1 = new hashmap <string、user>(); for(int i = 0; i <3; i ++){map1.put( ""+(i+10)、userArray1 [i]); } json = gson.tojson(map1); System.out.println( "ユーザーのマップコレクションはJSON文字列に変換されます、json ==="+json); System.out.println( "***************************"); system.out.println( "json文字列をBean->の配列オブジェクトに変換する); map <string、user> map2 = gson.fromjson(json、new typetoken <map <string、user >>(){}。getType()); system.out.println( "ユーザーの配列オブジェクトに変換、map2 =="+map2); }}実行結果:
1普通の豆の構成*********************************************************************************************** {name = fengjie; age = 12; sex = unknown; ischild = true}ユーザーオブジェクトはjson stringに変換されます、 json === {"name": "fengjie"、 "age": "age":12、 "sex": "nownow"、 "ischild":true} ***************************************変換********************************************** beanのリストコレクションをJSON文字列に変換 - >ユーザーのリストコレクションオブジェクトをJSON文字列に変換します。 json === [{"name": "ruhua"、 "age":10、 "sex": "male"、 "ischild":false}、{"name": "ruhua"、 "age":11、 "sex": "male"、 "ischild":fals}、{name ":" ruhua ":" " ****************************************************** json文字列を豆のリストコレクションに変換 - >ユーザーのリストコレクション、userlist2 = [{name = ruhua; age = 10; sex = male; ischild = false}、{name = ruhua; age = 11; {name = ruhua; age = 12; sex = mal; ischild = false}] 3Arrayアレイ変換****************************************************************************************************************************************************************************** json === [{"name": "hibi"、 "age":20、 "sex": "shemale"、 "ischild": "true}、{" name ":" hidden "、" age ":" sex ":" shemale "、" ischild ":true}、{name": "hidden": "sex": "sex" " ******************************************************* json文字列を豆の配列オブジェクトに変換 - >ユーザーの配列オブジェクトにユーザーに変換します。 ischild = true}、{name = hibiscus; age = 20; sex = hemale; ischild = true}、{name = hibiscus; age = 20; sex = hemale; ischild = true}] 4map変換*************************************************** json === {"10":{"name": "hibi"、 "age":20、 "sex": "shemale"、 "ischild":true}、 "11":{"name": "hibibi"、 "age": 20、 "sex": "shemale"、 "ischild":true}、 "12":{"name": "hibibi"、 "age":20、 "sex": "shemale"、 "ischild":true}}} *************************************************************************************************************************** {10 = 10 = {name = hibi; age = 20; sex = hermaid; ischild = true}、11 = {name = hibibi; sex = hischid; 12 = {name = hibibi; age = 20; sex = hermaid; ischild = true}}}