jackson框架解析json

Jackson解析的速度算是同类框架中最快的,同时也是Spring MVC中内置使用的解析方式。

准备工作:

下载jar包:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar

这个jar包在我的Demo中已经包含了,可以直接下载Demo:http://download.csdn.net/detail/u012251822/6877943

 

并将jar包添加到libs中

 

package com.example.jacksontest;

import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils {
	static ObjectMapper objectMapper;

	public static  T readValue(String content, Class valueType) {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}
		try {
			return objectMapper.readValue(content, valueType);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String toJSon(Object object) {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}
		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}

 


 

package com.example.jacksontest;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.TypeReference;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.jacksongtest.R;

public class MainActivity extends Activity {

	public static ObjectMapper objectMapper = new ObjectMapper();

	private TextView mBeanTextView;
	private TextView mBeanListTextView;
	private TextView mBeanMapTextView;
	private TextView mBeanMapListTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mBeanTextView = (TextView) findViewById(R.id.showBean);
		mBeanListTextView = (TextView) findViewById(R.id.showList);
		mBeanMapTextView = (TextView) findViewById(R.id.showMap);
		mBeanMapListTextView = (TextView) findViewById(R.id.showMapList);

		// 1.读取Bean类型json
		String resultStr = new String();
		String json = "{\"userName\": \"a\",\"password\":\"c\"}";
		resultStr = JsonUtils.readValue(json, User.class).toString();
		mBeanTextView.setText(resultStr);
		

		// 2.读取List类型json
		json = "[{\"userName\": \"a\",\"password\":\"c\"},{\"userName\": \"b\",\"password\":\"d\"}]";
		User[] users = JsonUtils.readValue(json, User[].class);
		List userList = Arrays.asList(users);
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < userList.size(); i++) {
			result.append(userList.get(i).toString() + "\t");
		}
		mBeanListTextView.setText(result);
		

		// 3.1.读取Map类型json:第一种方法TypeFactory
		json = "{\"A\":{\"userName\": \"a\",\"password\":\"c\"},"
				+ "\"B\":{\"userName\":\"b\",\"password\":\"d\"}}";
		Map usersMap = (Map) readJsonBeanInMap(
				json, String.class, User.class);
		Set keys = usersMap.keySet();
		result = new StringBuffer();
		for (Iterator it = keys.iterator(); it.hasNext();) {
			result.append(usersMap.get(it.next()).toString() + "\t");
		}
		mBeanMapTextView.setText(result);

		// 3.2.读取Map类型json:第二种方法TypeReference
		readJsonBeanInMap(json);
		

		// 4.读取List>类型json:TypeReference
		json = "[{\"A\":{\"userName\": \"a\",\"password\":\"a\"},\"C\":{\"userName\": \"c\",\"password\":\"c\"}},"
				+ "{\"B\":{\"userName\":\"b\",\"password\":\"b\"}}]";
		readJsonBeanInMapInList(json);

	}

	/**
	 * 读取Map<-Bean>类型json
	 */
	public void readJsonBeanInMap(String json) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			Map users = objectMapper.readValue(json,
					new TypeReference>() {
					});
			Set keys = users.keySet();
			StringBuffer result = new StringBuffer();
			for (Iterator it = keys.iterator(); it.hasNext();) {
				result.append(users.get(it.next()).toString() + "\t");
			}
			System.out.println(result);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取Map<-Bean>类型json
	 * 
	 * @param 
	 * @param 
	 */
	public  Map readJsonBeanInMap(String json, Class keyType,
			Class valueType) {
		try {
			Map map = objectMapper.readValue(json,
					TypeFactory.mapType(HashMap.class, keyType, valueType));
			return map;
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 读取List<-Map<-String,Bean>>类型json
	 */
	public void readJsonBeanInMapInList(String json) {
		try {
			List> users = objectMapper.readValue(json,
					new TypeReference>>() {
					});
			StringBuffer result = new StringBuffer();
			for (int i = 0; i < users.size(); i++) {
				Map tempMap = users.get(i);
				Set keys = tempMap.keySet();
				for (Iterator it = keys.iterator(); it.hasNext();) {
					result.append(tempMap.get(it.next()).toString() + "\t");
				}
			}
			mBeanMapListTextView.setText(result);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


 



    
    
    
    
    
    
    
     
    
    
    
     
    
    
    
     



 

 

Demo下载:http://download.csdn.net/detail/u012251822/6877943

 

你可能感兴趣的:(jackson框架解析json)