zoukankan      html  css  js  c++  java
  • json类型的相互转化

    package com.test.jsontest;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.commons.io.FileUtils;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    
    //map-json      json--file
    public class test {
    	public static void pp() throws JSONException, IOException {
    		Map<String,String> map=new HashMap();
    		map.put("1", "qw");
    		map.put("2", "qwrr");
    		JSONObject jb=new JSONObject(map);
    		System.out.println(jb);
    		FileWriter fileWriter=new FileWriter(new File("test.json"));
    		jb.write(fileWriter);
    		fileWriter.flush();
    	}
    	//对象-json
    	public static void pp1() {
    		stu stu=new stu();
    		stu.setId(1);
    		stu.setAdd(new address("huben","shiyi"));
    		JSONObject jb=new JSONObject(stu);
    		System.out.println(jb);
    		
    	}
    	//String-json
    	public static void pp2() {
    		String bb="{"name":"lisi"}";
    		JSONObject jb=new JSONObject(bb);
    		System.out.println(jb);
    		
    	}
    	//file-json
    	public  static void pp3() throws IOException {
    		InputStream inputStream=new FileInputStream(new File("E:\per.json"));
    
    		byte[]bytes=new byte[10];
    		int len=-1;
    		StringBuffer sBuffer=new StringBuffer();
    		while ((len=inputStream.read(bytes))!=-1) {
    			String string=new String(bytes);
    			sBuffer.append(string);
    		}
    		JSONObject jb=new JSONObject(sBuffer.toString());
    		System.out.println(jb);
    	}
    	//commons-io---json
    	public static void pp4() throws IOException {
    		String bb=FileUtils.readFileToString(new File("E:\per.json"));
    		
    		JSONObject jb=new JSONObject(bb);
    		System.out.println(jb);
    	}
    	//jsonarray
    	public static void pp5()  {
    		String sj="[{"name":"lisi"},{"name1":"zs"}]";
    		JSONArray jsonArray=new JSONArray(sj);
    		System.out.println(jsonArray);
    	}
    	
    	public static void pp6()  {
    		Map<String,String> map=new HashMap();
    		map.put("1", "qw");
    		map.put("2", "qwrr");
    		net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
    		jArray=jArray.fromObject(map);
    		System.out.println(jArray);
    	}
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    //		pp();
    //		pp1();
    //		pp2();
    //		pp3();
    //		pp4();
    //		pp5();
    		pp6();
    	}
    
    }
    
    
    
    package g1.g1;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    import net.sf.json.JSONObject;
    //map--jsonarray
    
    public class test_jsonarray {
    	public static void pp6()  {
    		Map<String,String> map=new HashMap();
    		map.put("1", "qw");
    		map.put("2", "qwrr");
    		net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
    		jArray=jArray.fromObject(map);
    		System.out.println(jArray);
    		System.out.println(map);
    	}
    
    	//jsonarray--map
    public static void pp7()  {
    			String ja="[{"1":"qw","2":"qwrr"},{"1":"qw","2":"qwrr"}]";
    			net.sf.json.JSONArray jArray=new net.sf.json.JSONArray();
    			jArray=jArray.fromObject(ja);
    			Map<String,Object> map=new HashMap();
    			System.out.println(jArray);
    			for(int i=0;i<jArray.length()-1;i++) {
    				
    				Object o=jArray.get(i);
    				JSONObject jObject=(JSONObject)o;
    				Iterator<String> iterator=jObject.keys();
    		        while (iterator.hasNext()) {
    		            String key = (String) iterator.next();
    		            String value = jObject.getString(key);
    		            map.put(key, value);
    		        	
    		        }
    			}
    			System.out.println(map);
    			
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    //		pp6();
    		pp7();
    	}
    
    }
    

      

  • 相关阅读:
    如何开始学习编程? 这 3 步很重要
    php正则表达式验证手机/固定电话/邮箱/身份证/银行卡自定义函数
    【经验分享】-PHP程序员的技能图谱
    PHP程序员的技能图谱
    PHP程序员要掌握的技能
    冒泡排序
    文件下载方法
    加密,解密方法
    获取真实IP
    二维数组根据某个字段排序
  • 原文地址:https://www.cnblogs.com/qinyios/p/10388957.html
Copyright © 2011-2022 走看看