json數組轉數組對象

import java.io.IOException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
	
	public static void main(String[] args) {
		//對應實體bean WorkDataInfo 不貼了
		 ObjectMapper objectMapper=new ObjectMapper();
		 String str =  "[{\"time\":\"2015-01-08 02:06:03\",\"title\":\"zy\",\"status\":-1,\"msgId\":\"f57b624d-7877-4974-94e3-1da77ad93fc0\",\"msgType\":1}]";

		// List<WorkDataInfo> list;
		try {
			WorkDataInfo[]	list =  objectMapper.readValue(str, WorkDataInfo[].class);              //直接輸出對象數組
			 for (WorkDataInfo workDataInfo : list) {
					System.out.println(workDataInfo.getTitle());
		}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
相關文章
相關標籤/搜索