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 java.io.IOException; import java.util.Arrays; 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.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); readJsonBean(); readJsonBeanInList(); readJsonBeanInMap(); readJsonBeanInMapInList(); } /** * 读取Bean类型json */ public void readJsonBean() { String json = "{\"userName\": \"a\",\"password\":\"c\"}"; try { User user = objectMapper.readValue(json, User.class); mBeanTextView.setText(user.toString()); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取List<-Bean>类型json */ public void readJsonBeanInList() { String json = "[{\"userName\": \"a\",\"password\":\"c\"},{\"userName\": \"b\",\"password\":\"d\"}]"; try { User[] users = objectMapper.readValue(json, User[].class); List<User> 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); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取Map<-Bean>类型json */ public void readJsonBeanInMap() { String json = "{\"A\":{\"userName\": \"a\",\"password\":\"c\"}," + "\"B\":{\"userName\":\"b\",\"password\":\"d\"}}"; ObjectMapper objectMapper = new ObjectMapper(); try { Map<String, User> users = objectMapper.readValue(json, new TypeReference<Map<String, User>>() { }); Set<String> keys = users.keySet(); StringBuffer result = new StringBuffer(); for (Iterator<String> it = keys.iterator(); it.hasNext();) { result.append(users.get(it.next()).toString() + "\t"); } mBeanMapTextView.setText(result); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取List<-Map<-String,Bean>>类型json */ public void readJsonBeanInMapInList() { String json = "[{\"A\":{\"userName\": \"a\",\"password\":\"a\"},\"C\":{\"userName\": \"c\",\"password\":\"c\"}}," + "{\"B\":{\"userName\":\"b\",\"password\":\"b\"}}]"; ObjectMapper objectMapper = new ObjectMapper(); try { List<Map<String, User>> users = objectMapper.readValue(json, new TypeReference<List<Map<String, User>>>() { }); StringBuffer result = new StringBuffer(); for (int i = 0; i < users.size(); i++) { Map<String, User> tempMap = users.get(i); Set<String> keys = tempMap.keySet(); for (Iterator<String> 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(); } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/showBeanTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bean" /> <TextView android:id="@+id/showBean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/showListTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/bean_list" /> <TextView android:id="@+id/showList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/showMapTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/bean_map" /> <TextView android:id="@+id/showMap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/showMapListTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/bean_map_list" /> <TextView android:id="@+id/showMapList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。