zoukankan      html  css  js  c++  java
  • JSON 与 对象 、集合 之间的转换

    1. JSON 格式

    对象格式

    {"name":"JSON","address":"北京市西城区","age":25}//JSON的对象格式的字符串

    对象数组格式

    [{"name":"JSON","address":"北京市西城区","age":25},{"name":"JSON","address":"北京市西城区","age":25}]//对象数组
    

    2. java 对象转 JSON 字符串

    fastjson

    JSONObject.toJSONString(result) // fastjson
    

    json-lib

               //1、使用JSONObject
            JSONObject json = JSONObject.fromObject(stu);
              //2、使用JSONArray
            JSONArray array=JSONArray.fromObject(stu);
            
            String strJson=json.toString();
            String strArray=array.toString();
    

    3. JSON字符串转 java对象

    fastjson

           String stuString = "{"age":2,"name":"公众号编程大道","sex":"m"}";
    
             //JSON字符串转换成Java对象
           Student student1 = JSONObject.parseObject(stuString, Student.class);

    json-lib

            JSONObject jsonObject=JSONObject.fromObject(objectStr);
            Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
            
            //2、使用JSONArray
            JSONArray jsonArray=JSONArray.fromObject(arrayStr);
            //获得jsonArray的第一个元素
            Object o=jsonArray.get(0);
            JSONObject jsonObject2=JSONObject.fromObject(o);
            Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
    

    4. list 转 json字符串

    json-lib

            //1、使用JSONObject
           // JSONObject listObject=JSONObject.fromObject(lists);
            //2、使用JSONArray
            JSONArray listArray=JSONArray.fromObject(lists);
    

    5. json字符串转 list(json-lib)

    从上面的例子可以看出list的对象只能转化为数组对象的格式

    //转化为list
    List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);
    
    //转化为数组
    Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
    

    6. map 转 json字符串(json-lib)

             //1、JSONObject
            JSONObject mapObject=JSONObject.fromObject(map);
          
            //2、JSONArray
            JSONArray mapArray=JSONArray.fromObject(map);
           
    

    7. json字符串转 map(json-lib)

           String strObject="{"first":{"address":"中国上海","age":"23","name":"JSON"}}";
            
            //JSONObject
            JSONObject jsonObject=JSONObject.fromObject(strObject);
            Map map=new HashMap();
            map.put("first", Student.class);
    
            //使用了toBean方法,需要三个参数 
            MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map);
            System.out.println(my.getFirst());
    

    MyBean

    import java.util.Map;
    
    import com.cn.study.day3.Student;
    
    public class MyBean {
        
        private Student first;
    
        public Student getFirst() {
            return first;
        }
    
        public void setFirst(Student first) {
            this.first = first;
        }
    
        
    
    }
    

    8.  JSON对象-->JSON字符串(fastjson)  

     Student stu = new Student("公众号编程大道", "m", 2);
    
           //先转成JSON对象
           JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
    
            //JSON对象转换为JSON字符串
          String jsonString = jsonObject.toJSONString();
    

    9.JSON对象-->Java对象(fastjson) 

     Student stu = new Student("公众号编程大道", "m", 2);
    
            //先转成JSON对象
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu);
            
            //JSON对象转换成Java对象
            Student student = JSONObject.toJavaObject(jsonObject, Student.class);
    

    10. JSON字符串-->JSON对象(fastjson) 

     String stuString = "{"age":2,"name":"公众号编程大道","sex":"m"}";
    
         //JSON字符串转换成JSON对象
         JSONObject jsonObject1 = JSONObject.parseObject(stuString);

      使用toBean()方法是传入了三个参数,第一个是JSONObject对象,第二个是MyBean.class,第三个是一个Map对象。通过MyBean可以知道此类中要有一个first的属性,且其类型为Student,要和map中的键和值类型对应,即,first对应键 first类型对应值的类型。

  • 相关阅读:
    FOJ2250 不可能弹幕结界
    寻找最大值
    Haybale Guessing
    MG loves string
    Curious Cupid
    Anton and Permutation
    TLE
    Jzzhu and Numbers
    Divisible Group Sums
    The merchant
  • 原文地址:https://www.cnblogs.com/jingjiren/p/13129878.html
Copyright © 2011-2022 走看看