fast-json 生成与解析json数据

 
  
 
  
package com.example.jsondata;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 fast-json 处理复杂的json数据(生成json、解析jsOn
 */
public class FastJsonActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_json);
        //生成Json数据
          toJson();
        //解析Json数据
         parseJson();
    }
     /*
        将封装的实体类数据生成Json格式的数据
      */
     private void toJson() {
         //生成一个单一的jsonObject 数据
         tojsonObject();
         //生成一个单一的jisonArray 数据
         tojsonArray();
         //生成一个jsonObjectjsonArray嵌套的json数据(外层jsonObject{ 内层jsonArray[]})
         tojsondata();
         //生成一个jsonObjectjsonArray嵌套的json数据(外层jsonArray[内层jsonObject{}])
         tojsondata2();
     }
    /*
      json格式的数据解析封装在实体类或集和中
     */
    private void parseJson() {
        //javaBean  将一个jsonObject字符串,解析成一个实体类对象
        parseToJavaBean();
        //List  将一个jsonArray,解析成一个泛型为实体类的List集合
        parseToList();
        //List 将一个复杂(嵌套)的json数据解析成一个泛型为mapList集合
        parseToListMap();

    }

    private String tojsonObject() {
        User user=new User("alittlestone","1742772822",'',"202028ma");
        String userJsonStr = JSON.toJSONString(user);
        Log.i("show","tojsOnString结果:"+userJsonStr);
        return userJsonStr;
    }
    private String tojsonArray() {
        List users1=new ArrayList<>();
        users1.add(new User("alittlewind","1737772822",'',"20202s2saa"));
        users1.add(new User("alittlestone","1742772822",'',"2020ajj1"));
        users1.add(new User("alittwater","1746215222",'',"2020sjj1"));
        String userJsonArrayStr = JSON.toJSONString(users1);//不会导致key值无序
        // Object user=JSON.toJSON(users1);  该方法和JSON.toJSONString() 返回值一样,但是key 值是无序的
        Log.i("show","生成jsonArray结果:"+userJsonArrayStr);
        return userJsonArrayStr;
    }
    private String  tojsondata() {
        List users=new ArrayList<>();
        users.add(new User("alittlewind","1737772822",'',"20202s2saa"));
        users.add(new User("alittlestone","1742772822",'',"2020ajj1"));
        users.add(new User("alittwater","1746215222",'',"2020sjj1"));
        UserGroup usergroup=new UserGroup(users,"北京",3);
        String group = JSON.toJSONString(usergroup);
        Log.i("show","嵌套json数据"+group);
        return group;
    }
    private String tojsondata2() {
        Map<String,User> map=new HashMap<>();
        map.put("user1",new User("alittlewind","1737772822",'',"20202s2saa"));
        map.put("user2",new User("ayahdh1jd","17727272822",'',"jaj1nsa"));
        Map<String,User> map2=new HashMap<>();
        map2.put("user3",new User("sjfjajfaa","118236667222",'',"fkaasllj"));
        ListString,User>> list=new ArrayList<>();
        list.add(map);
        list.add(map2);
        String jsondata2 = JSON.toJSONString(list);
        Log.i("show","jsondata:"+jsondata2);
        return jsondata2;
    }

    private void parseToJavaBean() {
        String obj = tojsonObject();
        User user = JSON.parseObject(obj, User.class);
        Log.i("show","jsonObject对象解析成实体类"+user);
    }

    private void parseToList() {
        String arr=tojsonArray();
        List users = JSON.parseArray(arr, User.class);
        Log.i("show","jsonObject对象解析成以实体类为泛型的集合"+users);
    }
    private void parseToListMap() {
        String str = tojsondata2();
        Type type=new TypeReferenceString,User>>>(){}.getType();
        ListString,User>> list= JSON.parseObject(str,type);
        Log.i("show","list"+list);
    }
}

 
  
 
  
 
  
/**
 * 封装的实体类 User类
 */

public class User {
    private String name;
    private String phone;
    private char sex;
    private String password;
    public User() {
    }

    public User(String name, String phone, char sex, String password) {
        this.name = name;
        this.phone = phone;
        this.sex = sex;
        this.password = password;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public char getSex() {
        return sex;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "user=name:"+name+",password:"+password+",phone:"+phone+",sex"+sex;
    }
}
 
  
 
  
/**
 * 
 封装的实体类 UserGroup类
*/
public class UserGroup {
    private List users;
    private String address;
    private int count;
    public UserGroup() {
    }

    public UserGroup(List users,String address,int count) {
        this.address=address;
        this.users = users;
        this.count=count;
    }
    public void setUsers(List users) {
        this.users = users;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List getUsers() {
        return users;
    }

    public String getAddress() {
        return address;
    }

    public int getCount() {
        return count;
    }

    @Override
    public String toString() {
        return "userGroup= users:"+users+",address:"+address+",count:"+count;
    }
}

 
  
 
  
 
  
 
 

你可能感兴趣的:(fast-json 生成与解析json数据)