zoukankan      html  css  js  c++  java
  • JSON字符串转为JSON对象

    在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是对象,所以,JSON对象(js对象)和JSON字符串之间的相互转换是关键。

    JSON可以有两种格式,一种是对象格式的,另一种是数组对象。

    //数据准备
    var jsonStr = '{"name":"dxm", "password":"1111"}';
    
    var jsonObj = {"name":"dxm", "password":"1111"};

    1、json字符串与js对象转换

    var jsonObj = $.parseJSON( jsonstr ); //jQuery提供,可以将json字符串转换成json对象
    var str = jsonObj.toJSONString();
    var jsonObj = eval('(' + jsonStr + ')'); //不推荐这些方式,这种方式不安全eval会执行json串中的表达式
    var jsonObj = JSON.parse(jsonStr); //JSON.parse(string)将字符串转为JSON格式;
    var jsonObj = JSON.stringify(jsonStr);//JSON.stringify(obj)将JSON转为字符串

    2、JSON 字符串 与 java 对象的转换

    转JSON字符串

    //使用JSONObject
    JSONObject json = JSONObject.fromObject(stu);
    //使用JSONArray
    JSONArray array=JSONArray.fromObject(stu);

    转java对象

    public static void jsonStrToJava(){
            //定义两种不同格式的字符串
            String objectStr="{"name":"JSON","age":"24","address":"HZ"}";
            String arrayStr="[{"name":"JSON","age":"24","address":"HZ"}]";
        
            //1、使用JSONObject
            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);
            System.out.println("stu:"+stu);
            System.out.println("stu2:"+stu2);
    }

    3、JSON 字符串 与集合的转换

    list:

    public static void listToJSON(){
            Student stu=new Student();
            stu.setName("JSON");
            stu.setAge("23");
            stu.setAddress("HZ");
            List<Student> lists=new ArrayList<Student>();
            lists.add(stu);
            //1、使用JSONObject
            //JSONObject listObject=JSONObject.fromObject(lists).toString();
            //2、使用JSONArray
            JSONArray listArray=JSONArray.fromObject(lists).toString();
            
        }
    public static void jsonToList(){
            String arrayStr="[{"name":"JSON","age":"24","address":"HZ"}]";
              //转化为list
               List<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);//转化为数组
               Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);
        }

    Map:

    public static void mapToJSON(){
            Student stu=new Student();
            stu.setName("JSON");
            stu.setAge("23");
            stu.setAddress("HZ");
            Map<String,Student> map=new HashMap<String,Student>();
            map.put("first", stu);
            
            //1、JSONObject
            JSONObject mapObject=JSONObject.fromObject(map).toString();//2、JSONArray
            JSONArray mapArray=JSONArray.fromObject(map).toString;
        }

    参考:https://www.cnblogs.com/zq-boke/p/5833387.html

       https://www.cnblogs.com/teach/p/5791029.html

  • 相关阅读:
    POJ 2923 Relocation (状态压缩,01背包)
    HDU 2126 Buy the souvenirs (01背包,输出方案数)
    hdu 2639 Bone Collector II (01背包,求第k优解)
    UVA 562 Dividing coins (01背包)
    POJ 3437 Tree Grafting
    Light OJ 1095 Arrange the Numbers(容斥)
    BZOJ 1560 火星藏宝图(DP)
    POJ 3675 Telescope
    POJ 2986 A Triangle and a Circle
    BZOJ 1040 骑士
  • 原文地址:https://www.cnblogs.com/by-dxm/p/8651990.html
Copyright © 2011-2022 走看看