1. 程式人生 > >JSON數組形式字符串轉換為List<Map<String,String>>的幾種方法

JSON數組形式字符串轉換為List<Map<String,String>>的幾種方法

字符 valid value 調用 rewind ise echo property 訪問

  package com.zkn.newlearn.json;
  
  import com.alibaba.fastjson.JSON;
  
  import com.alibaba.fastjson.JSONArray;
  
  import com.alibaba.fastjson.JSONObject;
  
  import java.util.List;
  
  import java.util.Map;
  
  /**
  
  * Created by zkn on 2016/8/22.
  
  */
  
  public class JsonToMapTest02 {
  
  public static void main(String[] args){

  
  String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
  
  "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]" ;
  
  //第一種方式
  
  List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
  
  System.out.println( "利用JSONArray中的parse方法來解析json數組字符串" );
  
  for (Map<String,String> mapList : listObjectFir){
  
  for (Map.Entry entry : mapList.entrySet()){
  
  System.out.println( entry.getKey() + " " +entry.getValue());
  
  }
  
  }
  
  //第二種方式
  
  List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List. class );
  
  System.out.println( "利用JSONArray中的parseObject方法並指定返回類型來解析json數組字符串" );
  
  for (Map<String,String> mapList : listObjectSec){
  
  for (Map.Entry entry : mapList.entrySet()){
  
  System.out.println( entry.getKey() + " " +entry.getValue());
  
  }
  
  }
  
  //第三種方式
  
  JSONArray listObjectThir = JSONArray.parseArray(strArr);
  
  System.out.println( "利用JSONArray中的parseArray方法來解析json數組字符串" );
  
  for (Object mapList : listObjectThir){
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
  
  }
  
  }
  
  //第四種方式
  
  List listObjectFour = JSONArray.parseArray(strArr,Map. class );
  
  System.out.println( "利用JSONArray中的parseArray方法並指定返回類型來解析json數組字符串" );
  
  for (Object mapList : listObjectFour){
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
  
  }
  
  }
  
  //第五種方式
  
  JSONArray listObjectFifth = JSONObject.parseArray(strArr);
  
  System.out.println( "利用JSONObject中的parseArray方法來解析json數組字符串" );
  
  for (Object mapList : listObjectFifth){
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
  
  }
  
  }
  
  //第六種方式
  
  List listObjectSix = JSONObject.parseArray(strArr,Map. class );
  
  System.out.println( "利用JSONObject中的parseArray方法並指定返回類型來解析json數組字符串" );
  
  for (Object mapList : listObjectSix){
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
  
  }
  
  }
  
  //第七種方式
  
  JSONArray listObjectSeven = JSON.parseArray(strArr);
  
  System.out.println( "利用JSON中的parseArray方法來解析json數組字符串" );
  
  for (Object mapList : listObjectSeven){
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
  
  foreach用法和之前的數組遍歷是一樣的,只不過這裏遍歷的key是屬性名,value是屬性值。在類外部遍歷時,只能遍歷到public屬性的,因為其它的都是受保護的,類外部不可見。
  
  class HardDiskDrive {
  
  public $brand;
  
  public $color;
  
  public $cpu;
  
  public $workState;
  
  protected $memory;
  
  protected $hardDisk;
  
  private $price;
  
  public function __construct($brand, $color, $cpu, $workState, $memory, $hardDisk, $price) {
  
  $this->brand = $brand;
  
  $this->color = $color;
  
  $this->cpu = $cpu;
  
  $this->workState www.qwert888.com= $workState;
  
  $this->memory = $memory;
  
  $this->hardDisk = $hardDisk;
  
  $this->price = $price;
  
  }
  
  }
  
  $hardDiskDrive = new HardDiskDrive(‘希捷‘, ‘silver‘, ‘tencent‘, ‘well‘, ‘1T‘, ‘hard‘, ‘$456‘);
  
  foreach ($hardDiskDrive as $property => $value) {
  
  var_dump($property, $value);
  
  echo ‘<br>‘;
  
  }
  
  輸出結果為:
  
  string(5) "brand" string(6) "希捷"
  
  string(5) "color" string(www.ysyl157.com) "silver"
  
  string(3) "cpu" string(7) "tencent"
  
  string(9) "workState" string(4) "well"
  
  通過輸出結果我們也可以看得出來常規遍歷是無法訪問受保護的屬性的。
  
  如果我們想遍歷出對象的所有屬性,就需要控制foreach的行為,就需要給類對象,提供更多的功能,需要繼承自Iterator的接口:
  
  該接口,實現了foreach需要的每個操作。foreach的執行流程如下圖:
  
  看圖例中,foreach中有幾個關鍵步驟:5個。
  
  而Iterator叠代器中所要求的實現的5個方法,就是用來幫助foreach,實現在遍歷對象時的5個關鍵步驟:
  
  當foreach去遍歷對象時, 如果發現對象實現了Ierator接口, 則執行以上5個步驟時, 不是foreach的默認行為, 而是調用對象的對應方法即可:
  
  示例代碼:
  
  class Team implements Iterator {
  
  //private $name =www.bais7.com ‘itbsl‘;
  
  //private $age = 25;
  
  //private $hobby = ‘fishing‘;
  
  private $info = [‘itbsl‘, 25, ‘fishing‘];
  
  public function rewind()
  
  {
  
  reset($this->info); //重置數組指針
  
  }
  
  public function valid(www.yigouyule2.cn/)
  
  {
  
  //如果為null,表示沒有元素,返回false
  
  //如果不為null,返回true
  
  return !is_null(key($this->info));
  
  }
  
  public function current(www.yisengyule.com)
  
  {
  
  return current($this->info);
  
  }
  
  public function key()
  
  {
  
  return key($this->info);
  
  }
  
  public function next()
  
  {
  
  return next($this->info);
  
  }
  
  }
  
  $team = new Team();
  
  foreach ($team as $property => $value) {
  
  var_dump($property, $value);
  
  echo ‘<br>‘;
  
  List listObjectEigh = JSONObject.parseArray(strArr,Map. class );
  
  System.out.println( "利用JSON中的parseArray方法並指定返回類型來解析json數組字符串" );
  
  for (Object mapList : listObjectEigh)www.18037.cn{
  
  for (Object entry : ((Map)mapList).entrySet()){
  
  System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());

JSON數組形式字符串轉換為List<Map<String,String>>的幾種方法