解析json格式 和拿Map 里面的键和值

时间:2022-10-31 18:38:16
package com.yanshu.utils;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.yanshu.tools.CRequest;




public class Test {


public static void main(String[] args) {



 String sts="[{\"inspectName\":\"张三\",\"sex\":\"男\",\"bornDate\":\"20170102\","
  + "\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\","
  + "\"accpetAddress\":\"河南省\",\"requireName\":\"王五\",\"requireAddress\":\"北京\","
  + "\"requirePhone\":\"137824556343\",\"orderNum\":\"20180201\","
  + "\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"张三\",\"sex\":\"女\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"旺旺\",\"requireAddress\":\"北京\",\"requirePhone\":\"137824556343\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"张三\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"旺旺\",\"requireAddress\":\"北京\",\"requirePhone\":\"137824556343\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"旺旺\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"旺旺\",\"requireAddress\":\"北京\",\"requirePhone\":\"137824556343\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"旺旺\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"Tom\",\"requireAddress\":\"上海徐汇\",\"requirePhone\":\"13781470678\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"小明\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"Tom\",\"requireAddress\":\"上海徐汇\",\"requirePhone\":\"13781470678\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"小明\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"Tom\",\"requireAddress\":\"上海徐汇\",\"requirePhone\":\"13781470678\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000},"
  + "{\"inspectName\":\"小明\",\"sex\":\"男\",\"bornDate\":\"20170102\",\"telephone\":\"13781470671\",\"acceptemail\":\"1378147067@qq.com\",\"accpetAddress\":\"河南省\",\"requireName\":\"Tom\",\"requireAddress\":\"上海徐汇\",\"requirePhone\":\"13781470678\",\"orderNum\":\"20180201\",\"taocanType\":\"A套餐\",\"taocanPrice\":10000}] ";
 
 System.out.println(sts);
 //把字符串转换成json格式
 
 JSONArray json=JSONArray.parseArray(sts);
 for(int i=0;i<json.size();i++) {

 JSONObject jsono=JSONObject.parseObject(json.get(i).toString());
 System.out.println("授权人:"
 +","+jsono.get("accpetAddress")+","+jsono.get("acceptemail")+","+jsono.get("bornDate")
 +","+jsono.get("inspectName")+","+jsono.get("sex")+","+jsono.get("telephone")+","+2);
 
 
 System.out.println("收货地址:"
 +","+jsono.get("requirePhone")+","+jsono.get("requireName")+","+jsono.get("requireAddress")+2);
 
 System.out.println("套餐选择:"
 +","+jsono.get("orderNum")+","+jsono.get("taocanPrice")+","+jsono.get("taocanType")+2);
 }
 


 System.out.println(json);

//方法一:中文操作系统中打印GBK   
       System.out.println(System.getProperty("file.encoding"));   
          
       //方法二:中文操作系统中打印GBK   
       System.out.println(Charset.defaultCharset());
       
       List list=new ArrayList<>();
      Map map=new HashMap<>();
      map.put("name", "张三");
      map.put("sex", 20);
      map.put("school","郑州大学");
      Map map1=new HashMap<>();
      map1.put("name", "tom");
      map1.put("sex", 20);
      map1.put("school","郑州大学");
      list.add(map);
      list.add(map1);
      String jsonString = JSON.toJSONString(list);
      System.out.println("list---->>"+jsonString);
      //开始解析json格式
      JSONArray array=new JSONArray(list);//json格式是以JSONArray json数组格式
      JSONArray array2=JSONArray.parseArray(jsonString);
      System.out.println(array2);
      System.out.println(array2.size());
      for(int z=0;z<array2.size();z++)
      {
      JSONObject jsono=JSONObject.parseObject(array2.get(z).toString());
      System.out.println(jsono.get("name"));
      }
      
      Map map2=new HashMap<>();
      map2.put("name", "张三");
      map2.put("sex", 20);
      map2.put("school","郑州大学");
       
      for(Object object:map2.entrySet())
      {
      Object key=((Map.Entry)object).getKey();
      Object vaObject=((Map.Entry)object).getValue();
      if(key=="name"||key.equals("name"))
      {
      String string=vaObject.toString();
      }
      }

}


}