android json解析

https://www.bilibili.com/video/av22724233/?p=13

 一、解析json

1.谷歌自带

JSONObject

try {
    JSONArray jsonArray = new JSONArray(result);
    JSONObject jsonObject=jsonArray.getJSONObject(0);
    mUserName=jsonObject.getString("USER_NAME");
    mRemark=jsonObject.getString("REMARK");
} catch (JSONException e) {
    e.printStackTrace();
}

 

opt对应的key不存在时返回null字符串或者指定值,get抛空指针异常

jsonObject.optString("REMARK");

 

 

2.GSON

Gson gson = new Gson();
//model要和json名称相同,model可以多于json的字段
ModelList = gson.fromJson(json, new TypeToken<List<xxxModel>>() {}.getType());

 

 

 

 

二、List转json

1.GSON

            Gson gson=new Gson();
            String str=gson.toJson(models);

 

三、GSONFormat插件

 

 alt+insert     选GsonFormat

 

 

四、fastjson

 

五、hijson小工具

posted @ 2018-12-13 10:39  富坚老贼  阅读(229)  评论(0编辑  收藏  举报