JSON数组形式字符串转换为List>的几种方法

  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());

上一篇:CS229 机器学习课程复习材料-线性代数


下一篇:Classifying with k-Nearest Neighbors(k近邻)