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

}

  

posted @ 2019-02-16 19:46  黑魔法os  阅读(405)  评论(0编辑  收藏  举报