记android学习之路----android中对json数据的解析

json:
1:json是javascript对象;
2:是一种数据格式;
3:用于传递数据
4:json非常轻量级,传输速度非常快;在接口数据传递中应用非常广泛;
5:android中对json的解析和封装是通过JSONObject和JSONArray来进行的;

JSONObject:
用于封装或者处理json数据的;
1:创建实例:
//创建一个空的JSONOBject实例,一般用于封装json数据
JSONOBject jsonObj = new JSONOBject();
//通过一个json字符串创建JSONOBject实例,一般用于解析json数据
JSONOBject jsonObj = new JSONOBject(String json);

JSONOBject jsonObj = new JSONOBject(Map copyFrom);

2:常用方法:
封装:
put(“key”,value);
//value是基本数据类型;
获取:
getXxx(“key”);
//Xxx是基本数据类型
getJSONArray();
getJSONObject();
判断:
has(“key”);
isNull();
length();
3:JSONArray:
用于操作json字符串中的数组:
基本形态:
“{[“name”,”age”,”password”]}”
“[“name”,”age”,”password”]”
“[{“name”:”tom”,”age”:20,”password”:1234}]”
1:创建实例:
//创建一个空的JSONArray实例,一般用于封装json数组
JSONArray jsonArr = new JSONArray();
//通过一个json字符串创建JSONArray实例,一般用于解析json数组
JSONArray jsonArr = new JSONArray(String json);

JSONArray jsonArr = new JSONArray(array arr);
2:常用方法:
封装:
    put(value);
    //value可以是基本数据类型;
获取:
    getXxx(int index);
    //Xxx是基本数据类型
    getJSONArray(int index);
    getJSONObject(int index);
    get(int index)
判断:
    isNull();
    length();

2:封装数据:
(1):
String jsonStr = {“name”:”tom”,”password”:123456}

JSONObject jsonStr = new JSONObject();
jsonStr.put("name","tom");
jsonStr.put("password",123456);

(2):
String jsonStr = {“name”:”tom”,”password”:123456,”group”:[“lili”,”join”]}

JSONArray jsonArr = new JSONArray();
jsonArr.put("lili");
jsonArr.put(join);
//或者
//String[] jsonArray = new String[]("lili","join");
//  JSONArray jsonArr = new JSONArray(jsonArray);


JSONObject jsonStr = new JSONObject();
jsonStr.put("name","tom");
jsonStr.put("password",123456);
jsonObj.put("group",jsonArr);

3:解析数据:
1:jsobj = {“name”:”tom”,”password”:123456}

JSONObject jsonObj = new JSONObject(jsobj);

jsonobj.getString(0);
jsonobj.getInt(1);

2:jsonStr = {"name":"tom","password":123456,"group":["lili","join"]}

JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray  jsarr = jsonObj.getJSONArray(group);
jsarr.getString(0);
jsarr.getString(1);

3:jsonStr = {"name":"tom","password":123456,"group":[{"name":"lili","age":20}]}

JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray  jsarr = jsonObj.getJSONArray(group);
JSONObject jsonObj2 = jsarr.getJSONObject(0);
String name1 = jsonObj2.getString("name");
int age = jsonObj2.getInt("age");

Gson:
1:下载Gson的jar包;
2:将其解压后放入libs目录;
3:Gson是google提供的一个解析和生成json的包;
4:作用原理就是:将json对象映射成java对象;
Gson:
1:创建Gson对象用于解析或者生成json:
1:Gson json = new Gson();
2:通过GsonBuilder 可以配置多种选项:
gson = new GsonBuilder()
.setLenient()// json宽松
.enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
.serializeNulls() //智能null
.setPrettyPrinting()// 调教格式
.disableHtmlEscaping() //默认是GSON把HTML 转义的
.create();

2:Gson中常用的方法:
    Gson.toJson(Object); 
    Gson.fromJson(Reader,Class);
    Gson.fromJson(String,Class);
    Gson.fromJson(Reader,Type);
    Gson.fromJson(String,Type);
3:常见应用:
    生成json串:
        1:将对象转为json串:
                class User{
                    public String name;
                    public int age;
                    public String home;

                    public user(na,ag,ho){
                        name = na;
                        age = ag;
                        home = ho;
                    }
                }

            Gson json  = new Gson();

            User user = new User("tom",120,"shenzhen");

            json.toJson(user);

            //{"name":"tom","age":120,"home":"shenzhen"}
    2:将List转为json字符串:
        List<String> list = new List<String>;

        list.add("tom");
        list.add("lili");

        json.toJson(list);

        //["tom","lili"]
    3:将Map转换为json子串:
        Map<String, Object> content = new HashMap<String, Object>();  
        content.put("name", "xuanyouwu");  
        content.put("age", "26");  
        gson.toJson(content): 
        //{"name":"xuanyouwu","age":"26"}  
 解析json子串:
    1:json子串转换为java对象:

        jsonStr = {"name":"xuanyouwu","age":26};  

        public class User{
            public String name;
            public int age;
        }

        User student1 = gson.fromJson(jsonStr, User.class);  
    2:将json转换为List;
        jsonStr = ["tom","lili","jone"]; 

    Type type = new TypeToken<ArrayList<String>>(){}.getType();   

    ArrayList<String> sList=gson.fromJson(listJsonStr, type);  

    3:

解析json数据:

1:jsonData = {“name”:”tom”,”age”:22,”home”:”shenzhen”}

1:创建一个类包含一些属性,属性名称和json数据的项名称一致:
如:

2:创建Gson对象:
    Gson json = new Gson();
    User user = json.fromJson(jsonData,User.class);
3:获取json数据:
    user.name:"tom";
    user.age:22;
    user.home:"shenzhen";

2:生成上述数据:

2:jsonData = {[“tom”,”lili”,”jone”]};
1:创建一个类包含一些属性,属性名称和json数据的项名称一致:
如:
class User{
public String name;
public int age;
public String home;
}
2:创建Gson对象:
Gson json = new Gson();
User user = json.fromJson(jsonData,new TypeToken

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值