json学习

Json基本使用

  <script type="text/javascript">
        //json的定义
        var jsonObj = {
            "key1": 12,
            "key2": "abc",
            "key3": true,
            "key4": [12, "qwe", true],
            "key5": {
                "key5_1": 12,
                "key5_2": "abc",
                "key5_3": true,
                "key5_4": [12, "qwe", true],
            },
            "key6": [{
                "key5_1": 12,
                "key5_2": "abc",
                "key5_3": true,
                "key5_4": [12, "qwe", true],
            }, {
                "key5_1": 12,
                "key5_2": "abc",
                "key5_3": true,
                "key5_4": [12, "qwe", true],
            }, {
                "key5_1": 12,
                "key5_2": "abc",
                "key5_3": true,
                "key5_4": [12, "qwe", true],
            }]
        }
         alert(jsonObj.key1);
        alert(jsonObj.key4);
       alert(jsonObj.key5.key5_1);
      var items = jsonObj.key6[0];
       alert(items.key5_1);
        /*json字符串格式*/
        var jsonString = JSON.stringify(jsonObj);//类似toString()
     alert(jsonString);
        /*json对象的格式*/
        var jsonParse= JSON.parse(jsonString);
        alert(jsonParse.key1);
    </script>
    <!--json的两种存在形式
          一种是对象,json对象的格式          一种是字符串,json字符串格式
  一般我们要操作json中数据的时候,需要json对象的格式
  一般在客户端和服务器之间进行的数据交换的时候,使用json字符串格式
  JSON.stringify()                         JSON.parse()
  -->

java中怎么使用Json

public class JsonTest {
    //1,javabean和json的转换
    @Test
    public void test1(){
        PerSon perSon = new PerSon(1,"Rieson");
        //创建Gson实例
        Gson gson = new Gson();
        //toJson方法可以把java对象转成字符串
        String s = gson.toJson(perSon);
        System.out.println(s);
        //fromJson把字符串转换回java对象
        //第一个参数是要转换的json字符串
        //第二个参数是转换回去的java对象类型
        PerSon perSon1 = gson.fromJson(s, PerSon.class);
        int id = perSon1.getId();
        String name = perSon1.getName();
        System.out.println(perSon1+name+id);

    }

    //1,List和json的转换
    @Test
    public void test2() {
        List<PerSon> list = new ArrayList<PerSon>();
        list.add(new PerSon(1,"haha"));
        list.add(new PerSon(2,"haha"));
        list.add(new PerSon(3,"haha"));
        Gson gson = new Gson();
        //list对象转换成json字符串
        String s = gson.toJson(list);
        //System.out.println(s);
        //json字符串转换成List对象
        List<PerSon> list1 = gson.fromJson(s, new PerSonListType().getType());//创建一个继承TypeToken的类,	      下方可以使用匿名内部类来实现
        PerSon perSon = list1.get(0);

        System.out.println(perSon);


    }

    //map和Json的转换
    @Test
    public void test3(){
        Map<String,PerSon> map = new HashMap<String,PerSon>();
        map.put("第一个人",new PerSon(1,"hah1"));
        map.put("第二个人",new PerSon(11,"hah11"));
        map.put("第三个人",new PerSon(111,"hah22"));
        //map对象转成json的字符串
        Gson gson = new Gson();
        String s = gson.toJson(map);
        System.out.println(s);
        //json字符串转换成map对象
      //  Map<String,PerSon> map1 = gson.fromJson(s, new PerSonMapType().getType());
        Map<String,PerSon> map1 = gson.fromJson(s, new TypeToken<HashMap<String,PerSon>>(){}.getType());
        PerSon o = map1.get("第一个人");

        System.out.println(o);
        System.out.println(map1);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值