String生成的json数据有两种 1 String json= " " ; JSON Array a = new JSON Array (json); System . out . println (a. toString ()); 2 String json2= "{'json': }" ; JSON Object b= new JSON Object (json2); System . out . println (b. toString ()); String类型的数据加“”,然后每个key,value加单引号。 JsonObject生成的数据有两种 1 JSON Array array = new JSON Array (); JSON Object object = new JSON Object (); JSON Object object1 = new JSON Object (); JSON Object obj= new JSON Object (); try { object . put ( "item1" , "value1" ); object . put ( "age" , 12 ); object . put ( "name" , "tom" ); object1. put ( "item2" , "value2" ); object1. put ( "age" , 12232 ); object1. put ( "name" , "tom" ); array. put ( object ); array. put (object1); obj. put ( "name" ,array); System . out . println (obj. toString ()); } catch ( Exception e){ } 结果:{ "name" : } 2 JSONArray array 1 = new JSONArray(); JSONObject object 2 = new JSONObject(); JSONObject object 3 = new JSONObject(); try { object 2 .put( "color" , "red" ); object 2 .put( "height" , 20 ); object 3 .put( "color" , "blue" ); object 3 .put( "height" , 1010 ); array 1 .put( object 2 ); array 1 .put( object 3 ); System.out.println(array 1 .toString()); }catch ( Exception e){ } 结果: 将集合生成json数据 1 Map < String , String map = new HashMap (); Map < String , String map 2 = new HashMap (); map.put( "name1" , "tom1" ); map.put( "age1" , "12" ); map 2 .put( "name1" , "tom1" ); map 2 .put( "age1" , "12" ); JSONObject object 4 = new JSONObject(); JSONArray array 2 = new JSONArray(); array 2 .put(map); array 2 .put(map 2 ); object 4 .put( "key" ,array 2 ); System.out.println( object 4 .toString()); 结果:{ "key" : } 2 Map < String , String map 1 = new HashMap (); map 1 .put( "as" , "adasd" ); map 1 .put( "asfa" , "afasff" ); JSONArray array 3 = new JSONArray(); array 3 .put(map 1 ); System.out.println(array 3 .toString()); 结果: 通过Gson生成json 一、单个对象生成json 生成以下类,该怎么生成呢? 1 2 3 4 5 6 { "createDate":"2015-02-01 10:39:50", "id":"1", "name":"传说之美", "password":"123456" } 先定义一个account类,属性有id、name、password、createDate。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 publicclassAccount { privateString id; privateString password; privateString name; privateString createDate; publicAccount() { super(); } publicAccount(String id, String password, String name, String createDate) { super(); this.id = id; this.password = password; this.name = name; this.createDate = createDate; } publicString getId() { returnid; } publicvoidsetId(String id) { this.id = id; } publicString getPassword() { returnpassword; } publicvoidsetPassword(String password) { this.password = password; } publicString getName() { returnname; } publicvoidsetName(String name) { this.name = name; } publicString getCreateDate() { returncreateDate; } publicvoidsetCreateDate(String createDate) { this.createDate = createDate; } @Override publicString toString() { return"Account \n\n"; } } 定义好这个类,就可以利用Gson生成json字符串了。 1 2 3 4 5 6 7 8 // 生成account对象 SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Account account =newAccount("1","123456","传说之美", sdf.format(newDate())); // 利用gson对象生成json字符串 Gson gson =newGson(); String jsonString = gson.toJson(account); Log.i("", jsonString); 输入的log如下 二、解析json字符串 为 单个对象 在上面已生成了jsonString,那如何将其解析为单个对象,很简单。 1 2 3 // 利用gson解析json字符串为单个对象 Account account1 = gson.fromJson(jsonString, Account.class); Log.i("", account1.toString()); 看看输出的log 三、生成单个对象的json数组 什么事json数组,类似下面的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 生成json数组代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Account account2 =newAccount("2","123456","传说", sdf.format(newDate())); Account account3 =newAccount("2","123456","之美", sdf.format(newDate())); List accountList =newArrayList (); accountList.add(account2); accountList.add(account3); JSONArray accountArray =newJSONArray(); for(inti =0; i < accountList.size(); i++) { String accountStr = gson.toJson(accountList.get(i)); JSONObject accountObject; try{ accountObject =newJSONObject(accountStr); accountArray.put(i, accountObject); }catch(JSONException e) { e.printStackTrace(); } } Log.i("", accountArray.toString()); log的输出为 四、由多个单个对象的json数组解析为对个单个对象 多个单个对象组成的json数组解析如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // 解析json数组 List accountList2 =newArrayList (); for(inti=0;i