立即注册 登录
About云-梭伦科技 返回首页

virgo的个人空间 https://aboutyun.com/?4176 [收藏] [复制] [分享] [RSS]

日志

在android端Jackson解析json

已有 1550 次阅读2014-5-18 11:33

1、  前面的日志介绍了如何读取json,下面继续介绍如何解析json,之前采用的opt方法有一个缺陷,如果对象值为空的时候,容易出现获取异常。所以应该改变之前的解析方式。

2、  Json主要是maplist的混合,获取的json有几种类型:

A:单一的jsonObject

B:单一的jsonArray

C:混合的模式,jsonObject里面包含jsonArray,或者jsonArray里面包含jsonObject

3、  本例子中采用的是volley里面获取jsonObject对象的方法,所以主要演示 jsonObject对象的解析,但是包含了jsonArray,下面演示一下:

首先在jackson官网下载jar包,添加到libs里面,http://wiki.fasterxml.com/JacksonDownload

AJson数据的格式如下:

{"findandlost":[{"category":"","content":"本人于201434日文泰201丢失黑色冒牌苹果钱包,内有身份证及几张银行卡。望有好心人捡到与本人联系,本人学号1109020234  刘芳,必有重谢!联系电话 18702872345","createTime":"2014-03-05 10:47:35","flag":"寻物","loastandfindId":"01","name":"刘芳","state":"寻物"},{"category":"","content":"本人129日在文波将一挎肩包遗失在坐上,包内有驾驶证、钱包、身份证银行卡等物品。有拾到者请于本人联系,本人学号1109020247 林强,愿意1000元现金作为酬谢。联系电话 18702872387","createTime":"2014-01-29 10:47:40","flag":"寻物","loastandfindId":"02","name":"刘芳","state":"寻物"}]}

B:解析的源代码:

  private void getJson(){

     String url = "http://192.168.20.1:8080/xiaoyuantong/findandlostAction!getFindAndLostAll.action";

                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(

                   Request.Method.GET, url, null,

                   new Response.Listener<JSONObject>() {

                       @Override

                       public void onResponse(JSONObject response) {

                     

                           Log.e("bbb", response.toString());

                          

                           ObjectMapper objectMapper = new ObjectMapper();

                     

                           JSONArray json = null;

                       

                            try {

                                //直接获取了jsonArray

                                json = response.getJSONArray("findandlost");

//jsonArray转换为list

                                 List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json.toString(), List.class);

                                    System.out.println(list.size());

                                    for (int i = 0; i < list.size(); i++) {

                                        Map<String, Object> map = list.get(i);

                                        Set<String> set = map.keySet();

                                        for (Iterator<String> it = set.iterator();it.hasNext();) {

                                            String key = it.next();

                                            System.out.println(key + ":" + map.get(key));

                                         

                                        }

                                    }

                               

                               

                            } catch (JSONException e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                            }

                           //根据前面的标注获取字符串

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

                            }

 

 

                               }

                      

                   }, new Response.ErrorListener() {

                       @Override

                       public void onErrorResponse(VolleyError arg0) {

                           // System.out.println("sorry,Error");

                           Log.e("aaa", arg0.toString());

                       }

                   });

           requestQueue.add(jsonObjectRequest);

   

    }

   

   

C:在前端获取到的效果:

 

D:解析以后的效果:

4、  如果是jsonObject对象,直接映射为map

Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class);

    System.out.println(maps.size());

    Set<String> key = maps.keySet();

    Iterator<String> iter = key.iterator();

    while (iter.hasNext()) {

        String field = iter.next();

        System.out.println(field + ":" + maps.get(field));

}

5、但是很多情况下,我们希望直接转换成我们需要的对象,而不用把一个个获取的值填入对象里面。


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条