使用JQuery解析、处理JSON数据(应用在课程表)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">

var DataSource = [
                    {"weekday":1,"course":[
                        {"courseId":1,"courseName":"语文","pointName":1},
                        {"courseId":2,"courseName":"英语","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":1,"courseName":"语文","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]},
                    {"weekday":2,"course":[
                        {"courseId":2,"courseName":"英语","pointName":1},
                        {"courseId":1,"courseName":"语文","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":-1,"courseName":"不安排课程","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]},
                    {"weekday":3,"course":[
                        {"courseId":3,"courseName":"数学","pointName":1},
                        {"courseId":2,"courseName":"英语","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":1,"courseName":"语文","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]},
                    {"weekday":4,"course":[
                        {"courseId":1,"courseName":"语文","pointName":1},
                        {"courseId":2,"courseName":"英语","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":1,"courseName":"语文","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]},
                    {"weekday":5,"course":[
                        {"courseId":1,"courseName":"语文","pointName":1},
                        {"courseId":2,"courseName":"英语","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":1,"courseName":"语文","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]},
                    {"weekday":6,"course":[
                        {"courseId":1,"courseName":"语文","pointName":1},
                        {"courseId":2,"courseName":"英语","pointName":2},
                        {"courseId":3,"courseName":"数学","pointName":3},
                        {"courseId":4,"courseName":"自习","pointName":4},
                        {"courseId":1,"courseName":"语文","pointName":5},
                        {"courseId":2,"courseName":"英语","pointName":6},
                        {"courseId":3,"courseName":"数学","pointName":7},
                        {"courseId":4,"courseName":"自习","pointName":8}
                    ]}

            ];
/**********************************************************************************************************************/
//查询周六第8节课
var v_weekday=6;
var v_pointName=8;

$.each(DataSource,function(item)
{
    if(DataSource[item].weekday==v_weekday)
    {
        $.each(DataSource[item].course,function(o)
            {
                if(DataSource[item].course[o].pointName==v_pointName)
                {
                    console.log(DataSource[item].course[o]);
                    return;
                }
            });
        return;
    }
});
/**********************************************************************************************************************/
//添加周六第9节自习课程
var o = new Object();
o.courseId = 4;
o.courseName ="自习";
o.pointName =9;

$.each(DataSource,function(item) {
    if(DataSource[item].weekday==v_weekday)
        {
            DataSource[item].course.push(o);
            return;
        }
    });

//输出添加完成的结果
console.log(DataSource);
/**********************************************************************************************************************/
//删除一条数据
$.each(DataSource,function(item)
{
    if(DataSource[item].weekday==v_weekday)
        {
            for (var i = 0; i < DataSource[item].course.length; i++)
            {
                var c = DataSource[item].course[i];
                if (c.pointName == v_pointName)
                {
                    DataSource[item].course.splice(i, 1);
                }
            }
            return;
        }
});
console.log(DataSource);
/**********************************************************************************************************************/
</script>
</head>

<body>

</body>
</html>
时间: 2024-10-09 14:47:27

使用JQuery解析、处理JSON数据(应用在课程表)的相关文章

用JQuery解析获取JSON数据

JSON 是一种比较方便的数据形式,下面使用$.getJSON方法,实现获得JSON数据和解析,都挺方便简单的.从http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=? 这个地址得到JSON数据 并且分析里面的结构,生成图片和相关链接等: $(function(){ var url="http://api.flickr.co

jQuery异步获取json数据的2种方式

jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: { "one" : "Hello", "two" : "World" } ■ 通过$.getJSON方法获取json数据 <script src="Scripts/jquery-2.1.1.min.js"

Java解析(读取)Json数据

以前看过书上说,XML是web service间传输信息的标准格式吧,就看了看XML.最近在做个网站,又说是有了JSON,第一回听说就看了看,总结总结一下. 1.JSON介绍 JSON比XML简单,主要体现在传输相同信息的情况下,文件的大小不同. JSON只用于传输信息,XML还可以用于配置文件的使用. JSON中的符号主要有: " , [ {: 2.JSON中的数组和对象 2.1数组(JSONArray) 数组用一对[],表示存放的是一般的数组数据. 如:["11",&qu

利用Jquery获取服务器端json数据

json (JavaScript Object Notation)是一种轻量级的数据交换格式,它使用的是的http get请求 通过jQuery的.getJSON()方法获取json数据,并转化为字符串 $.getJSON("/json/cats.json",function(json){        $(".message").html(JSON.stringify(json));      }); });

模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。

需要用到的lib包 :解析json  gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三方框架  点击下载 侧滑菜单的lib  点击下载 1 package com.lixu.testjsonall; 2 3 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; 4 import com.jeremyfeinstein.slid

JQuery UI获取JSON数据

最近在学习用JQuery UI开发一个界面,这是javascript的一个框架,功能强大,界面漂亮. 首先要下载JQuery UI的包,引入里面的文件: <link href="jquery-ui.css" rel="stylesheet"> <script src="external/jquery/jquery.js"></script> <script src="jquery-ui.js&q

Java解析复杂JSON数据的一种方法

1.需解析JSON数据: { "code": 0, "message": "success", "sid": "[email protected]", "data": { "result": { "age": { "age_type": "0", "child": "0.1452

Android解析服务器Json数据实例

Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 2012, "rating": 7.8, "duration": "141 min", "director": "Joss Whedon", "tagline": "A new age

jQuery遍历多层json数据

1.json与jsonp的区别(待查) 2.要遍历的数据如下: {"status": "ok", "code": 200, "data": {"343946": {"8144": {"feed": 0, "instagram": 0, "livefyre": 21, "facebook": 0, "

C#解析多层Json数据

[事件回顾] 今天在做一个小项目的时候,想获取一下位置,IP地址,然后随便在网上找了一个api调用 https://apis.map.qq.com/ws/location/v1/ip 数据如下: 如果是用Python的话,很简单,请求API->得到Json->然后print(["result"]["ip"])就可以了,C#有点麻烦,用了多个方法来解决 前提是先引入 using Newtonsoft.Json.Linq;,或者 using Newtonso