语法值获取、JSON解析器对象转换

语法值获取

获取数据:

  json对象,建名

  json对象[“键名”]

  数组对象[索引]

案例:

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        //1.定义基本格式
        var person = {"name": "张三", age: 23, 'gender': true};

        //获取name的值
        //var name = person.name;
        var name = person["name"];
       // alert(name);

        //alert(person);
        //2.嵌套格式   {}———> []
        var persons = {
            "persons": [
                {"name": "张三", "age": 23, "gender": true},
                {"name": "李四", "age": 24, "gender": true},
                {"name": "王五", "age": 25, "gender": false}
                ]
        };
       // alert(persons);
        //获取王五值
        var name1 = persons.persons[2].name;
       // alert(name1);


        //2.嵌套格式   []———> {}
        var ps = [{"name": "张三", "age": 23, "gender": true},
            {"name": "李四", "age": 24, "gender": true},
            {"name": "王五", "age": 25, "gender": false}];
        //获取李四值
        //alert(ps);
        alert(ps[1].name);


    </script>

</head>
<body>

</body>
</html>

JSON解析器对象转换

JSON解析器:

  常见的解析器:Jsonlib,Gson,fastjson,Jackson

JSON转为java对象

java对象转换JSON

  使用步骤:

    导入jackson的相关jar包

    创建Jasckson核心对象ObjectMapper

    调用ObjectMapper的相关方法进行转换

posted @ 2022-08-24 09:09  一位程序袁  阅读(12)  评论(0编辑  收藏  举报