JavaScript对json文件的读操作、遍历操作、清洗【json】

2023-05-25 14:37:11 浏览数 (2)

关于前后端交互中,前端常用的js处理Ajax接收到的json数据,进行后续的读取、遍历操作,以实现页面数据填充等。 常见场景(以微博热搜为例),

json读操作

先,从接口处爬下来

代码语言:javascript复制
url ='https://weibo.com/ajax/side/hotSearch'
headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
    }
    # 响应
    r = requests.get(url,headers)

再,存储为文本一定要保存为json字符形式,保存r.json去json.cn上都解析不出来

代码语言:javascript复制
    with open('D:/Temp/hot_search_ranking/data/data.json','w',encoding='utf-8') as fp:
        fp.write(r.text)

json字符形式(如下,键值对都带引号)

代码语言:javascript复制
{"label_name":"","star_word":0,"word":"法国发现新冠新变异毒株IHU","category":"社会新闻","num":1282883,"subject_querys":"","flag":0,"onboard_time":1641383803,"raw_hot":1282883,"note":"法国发现新冠新变异毒株IHU","emoticon":"","ad_info":"","mid":"4722363411727060","realpos":1,"topic_flag":1,"channel_type":"","fun_word":0,"subject_label":"","rank":0}

下次,用的时候从文本中读取用的时候使用r.json,也就是用json对象

代码语言:javascript复制
realTimeContent = r.json()['data']['realtime']

json 数据传回前端往往需要

代码语言:javascript复制
    with open('D:/Temp/hot_search_ranking/data/data.json','r',encoding='utf-8') as fp:
        r = fp.read()#读json文件
        r = json.loads(r)['data']['realtime']#转json对象
        r = json.dumps(r)#转json文本,和json对象一模一样,不是json字符形式,它的键没有双引号,这是传输不能直接传json对象的才传文本的缘故
    return r

这样传输的是二进制文本

代码语言:javascript复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* [{"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u897fu5b89u6709u5c0fu533au5f00u59cbu7edfu8ba1u5b55u5987u4fe1u606f", "category": "u793eu4f1au65b0u95fb", "num": 1545054, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 1545054, "note": "u897fu5b89u6709u5c0fu533au5f00u59cbu7edfu8ba1u5b55u5987u4fe1u606f", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722340174762648", "onboard_time": 1641390467, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 1, "rank": 0}, {"label_name": "u70ed", "star_word": 0, "word": "u897fu5b89u8981u6c42u5bf9u5b55u4ea7u5987u7b49u5f00u901au7effu8272u901au9053", "category": "u793eu4f1au65b0u95fb", "num": 1014694, "subject_querys": "", "flag": 2, "icon_desc": "u70ed", "raw_hot": 1014694, "note": "u897fu5b89u8981u6c42u5bf9u5b55u4ea7u5987u7b49u5f00u901au7effu8272u901au9053", "emoticon": "", "is_hot": 1, "icon_desc_color": "#ff9406", "mid": "4722331668186725", "realpos": 2, "onboard_time": 1641376243, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 1}, {"label_name": "", "star_word": 0, "word": "u628au63e1u5386u53f2u4e3bu52a8u521bu9020u65b0u7684u4f1fu4e1a", "category": "u56fdu5185u8981u95fb", "num": 978123, "subject_querys": "", "flag": 0, "onboard_time": 1641303290, "raw_hot": 621867, "note": "u628au63e1u5386u53f2u4e3bu52a8u521bu9020u65b0u7684u4f1fu4e1a", "emoticon": "", "ad_info": "", "mid": "4722032266973549", "realpos": 3, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 2}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u51b0u51b0u5728u51b0u51b0u7684u51b0u4e0au6ed1u51b0", "category": "u793eu4f1au65b0u95fb", "num": 975276, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 1762069, "note": "u51b0u51b0u5728u51b0u51b0u7684u51b0u4e0au6ed1u51b0", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722383736541462", "onboard_time": 1641391729, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 4, "rank": 3}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u4ee4u4ebau5fc3u52a8u7684offeru674eu4e0du8a00", "category": "u7efcu827a", "num": 975004, "realpos": 5, "flag": 1, "icon_desc": "u65b0", "raw_hot": 1126361, "note": "u4ee4u4ebau5fc3u52a8u7684offeru674eu4e0du8a00", "emoticon": "", "subject_label": "u7efcu827a", "icon_desc_color": "#ff3852", "mid": "4722364576699155", "flag_desc": "u7efcu827a", "subject_querys": "u7efcu827a|u4ee4u4ebau5fc3u52a8u7684offer", "ad_info": "", "topic_flag": 0, "channel_type": "Entertainment", "fun_word": 1, "onboard_time": 1641392211, "rank": 4}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5c0fu7ea2u4e66u516cu5e03u7b2cu4e8cu6279u8fddu89c4u8425u9500u54c1u724cu5904u7f5au540du5355", "category": "u793eu4f1au65b0u95fb", "num": 969185, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 969185, "note": "u5c0fu7ea2u4e66u516cu5e03u7b2cu4e8cu6279u8fddu89c4u8425u9500u54c1u724cu5904u7f5au540du5355", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722333140650145", "onboard_time": 1641391607, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 6, "rank": 5}, {"icon_type": "kong", "id": 144586, "forbidden": {}, "icon": "", "is_ad": 1, "is_topic": 1, "monitors": {"app": {}, "pc": {}}, "end_time": 1641398400, "sort": 2, "is_star": 1, "name": "#u5f20u667au9716u6768u6021u65b0u5267u597du6293u9a6c#", "ad_type": "u8d44u6e90u6295u653e", "num": 927755, "dot_icon": 1, "word": "u5f20u667au9716u6768u6021u65b0u5267u597du6293u9a6c", "ad_channel": 2, "note": "u5f20u667au9716u6768u6021u65b0u5267u597du6293u9a6c", "topic_flag": 1, "monitor": {}, "start_time": 1641384000, "poi": 6, "rank": 5}, {"label_name": "u70ed", "star_word": 0, "word": "u9662u65b9u56deu5e94u5b55u5987u5728u533bu9662u95e8u53e3u7b492u5c0fu65f6u540eu6d41u4ea7", "category": "u793eu4f1au65b0u95fb", "num": 924636, "subject_querys": "", "flag": 2, "icon_desc": "u70ed", "raw_hot": 924636, "note": "u9662u65b9u56deu5e94u5b55u5987u5728u533bu9662u95e8u53e3u7b492u5c0fu65f6u540eu6d41u4ea7", "emoticon": "", "is_hot": 1, "icon_desc_color": "#ff9406", "mid": "4722304963055142", "realpos": 7, "onboard_time": 1641368746, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 6}, {"label_name": "", "star_word": 0, "word": "u90bbu5c45u5403u69b4u83b2u7537u5b50u4ee5u4e3au7164u6c14u6cc4u6f0fu62a5u706bu8b66", "category": "u793eu4f1au65b0u95fb", "num": 768248, "subject_querys": "", "flag": 0, "onboard_time": 1641387406, "raw_hot": 768248, "note": "u90bbu5c45u5403u69b4u83b2u7537u5b50u4ee5u4e3au7164u6c14u6cc4u6f0fu62a5u706bu8b66", "emoticon": "", "ad_info": "", "mid": "4722354066033913", "realpos": 8, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 7}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u8881u5f18u547cu5401u7537u540cu80deu4ee5u8463u535au5b87u4e3au6212", "category": "u5f71u89c6", "num": 720575, "realpos": 9, "flag": 1, "icon_desc": "u65b0", "raw_hot": 720575, "note": "u8881u5f18u547cu5401u7537u540cu80deu4ee5u8463u535au5b87u4e3au6212", "emoticon": "", "subject_label": "u5267u96c6", "icon_desc_color": "#ff3852", "mid": "4722356561383204", "flag_desc": "u5267u96c6", "subject_querys": "u5267u96c6|u5e78u798fu4e8cu91cdu594f", "ad_info": "", "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "onboard_time": 1641388723, "rank": 8}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5510u827au6615u8bf4u7231u60c5u9700u8981u5f7cu6b64u643au624bu5f25u8865u7f3au53e3", "category": "u7efcu827a", "num": 655500, "realpos": 10, "flag": 1, "icon_desc": "u65b0", "raw_hot": 655500, "note": "u5510u827au6615u8bf4u7231u60c5u9700u8981u5f7cu6b64u643au624bu5f25u8865u7f3au53e3", "emoticon": "", "subject_label": "u7efcu827a", "icon_desc_color": "#ff3852", "mid": "4722384537387643", "flag_desc": "u7efcu827a", "subject_querys": "u7efcu827a|u534au719fu604bu4eba", "ad_info": "", "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "onboard_time": 1641390529, "rank": 9}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5916u5a46u7ed910u4e2au6708u5a74u513fu522eu75e7u81f4u5176u9762u90e8u6e83u70c2", "category": "u793eu4f1au65b0u95fb", "num": 645551, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 910115, "note": "u5916u5a46u7ed910u4e2au6708u5a74u513fu522eu75e7u81f4u5176u9762u90e8u6e83u70c2", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722376942551313", "onboard_time": 1641392211, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 11, "rank": 10}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u4e00u5bb6u4e09u53e3u5367u5ba4u70e4u706bu81f4u4e00u6c27u5316u78b3u4e2du6bd2", "category": "u793eu4f1au8d1fu9762", "num": 639352, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 821268, "note": "u4e00u5bb6u4e09u53e3u5367u5ba4u70e4u706bu81f4u4e00u6c27u5316u78b3u4e2du6bd2", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722404225975226", "onboard_time": 1641391550, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 12, "rank": 11}, {"label_name": "u70ed", "star_word": 1, "word": "u91d1u7f8eu79c0u53bbu4e16", "category": "u827au4eba", "num": 639147, "subject_querys": "", "flag": 2, "icon_desc": "u70ed", "raw_hot": 656722, "note": "u91d1u7f8eu79c0u53bbu4e16", "emoticon": "[u6cea]", "is_hot": 1, "icon_desc_color": "#ff9406", "mid": "4722296042555807", "realpos": 13, "onboard_time": 1641369043, "ad_info": "", "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "", "rank": 12}, {"label_name": "", "star_word": 0, "word": "u6ca1u6709u5c0fu670bu53cbu80fdu62d2u7eddu8fd9u4e2au793cu7269", "category": "u5e7du9ed8", "num": 630724, "subject_querys": "", "flag": 0, "onboard_time": 1641386684, "raw_hot": 630724, "note": "u6ca1u6709u5c0fu670bu53cbu80fdu62d2u7eddu8fd9u4e2au793cu7269", "emoticon": "", "ad_info": "", "mid": "4722312522499377", "realpos": 14, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 13}, {"label_name": "", "star_word": 0, "word": "u6cd5u56fdu53d1u73b0u65b0u51a0u65b0u53d8u5f02u6bd2u682aIHU", "category": "u793eu4f1au65b0u95fb", "num": 622929, "subject_querys": "", "flag": 0, "onboard_time": 1641383803, "raw_hot": 622929, "note": "u6cd5u56fdu53d1u73b0u65b0u51a0u65b0u53d8u5f02u6bd2u682aIHU", "emoticon": "", "ad_info": "", "mid": "4722363411727060", "realpos": 15, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 14}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5e02u76d1u603bu5c40u5bf9u963fu91ccu817eu8bafu54d4u54e9u54d4u54e9u505au51fau884cu653fu5904u7f5a", "category": "u793eu4f1au65b0u95fb", "num": 564688, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 564688, "note": "u5e02u76d1u603bu5c40u5bf9u963fu91ccu817eu8bafu54d4u54e9u54d4u54e9u505au51fau884cu653fu5904u7f5a", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722236369930042", "onboard_time": 1641389806, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 16, "rank": 15}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u660eu540eu4e24u5929u5c06u76f8u7ee7u4e0au6f14u795eu79d8u5929u8c61", "category": "u793eu4f1au65b0u95fb", "num": 464865, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 464865, "note": "u660eu540eu4e24u5929u5c06u76f8u7ee7u4e0au6f14u795eu79d8u5929u8c61", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722331438287915", "onboard_time": 1641390646, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 17, "rank": 16}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u7537u5b69u522eu9c7cu9cdeu5200u5de5u4e0du4e00u822c", "category": "u793eu4f1au65b0u95fb", "num": 454099, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 454099, "note": "u7537u5b69u522eu9c7cu9cdeu5200u5de5u4e0du4e00u822c", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722294179499318", "onboard_time": 1641388966, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 18, "rank": 17}, {"label_name": "", "star_word": 0, "word": "u90d1u5ddeu65b0u589e6u4f8bu9633u6027", "category": "u793eu4f1au65b0u95fb", "num": 453908, "subject_querys": "", "flag": 0, "onboard_time": 1641386381, "raw_hot": 453908, "note": "u90d1u5ddeu65b0u589e6u4f8bu9633u6027", "emoticon": "", "ad_info": "", "mid": "4722375696845680", "realpos": 19, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 18}, {"label_name": "", "star_word": 0, "word": "u6d59u6c5fu65b0u589e3u4f8bu672cu571fu786eu8bca", "category": "u793eu4f1au65b0u95fb", "num": 426021, "subject_querys": "", "flag": 0, "onboard_time": 1641386021, "raw_hot": 426021, "note": "u6d59u6c5fu65b0u589e3u4f8bu672cu571fu786eu8bca", "emoticon": "", "ad_info": "", "mid": "4722393510054906", "realpos": 20, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 19}, {"label_name": "u70ed", "star_word": 0, "word": "u5b89u5fbdu660eu786eu518du5a5au5bb6u5eadu53efu518du751fu80b2u4e09u4e2au5b50u5973", "category": "u793eu4f1au65b0u95fb", "num": 421766, "subject_querys": "", "flag": 2, "icon_desc": "u70ed", "raw_hot": 777817, "note": "u5b89u5fbdu660eu786eu518du5a5au5bb6u5eadu53efu518du751fu80b2u4e09u4e2au5b50u5973", "emoticon": "", "is_hot": 1, "icon_desc_color": "#ff9406", "mid": "4722318469760524", "realpos": 21, "onboard_time": 1641379543, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 20}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u738bu4e00u535au5effu8de8u5e74u665au4f1au5f69u6392u82b1u7d6eu89c6u9891", "category": "u7efcu827a", "num": 414930, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 513477, "note": "u738bu4e00u535au5effu8de8u5e74u665au4f1au5f69u6392u82b1u7d6eu89c6u9891", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722401793805457", "onboard_time": 1641392510, "ad_info": "", "topic_flag": 0, "channel_type": "Entertainment", "fun_word": 1, "realpos": 22, "rank": 21}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u897fu5b89u5168u5e02u6b63u5728u96c6u4e2du9694u79bb42000u4f59u4eba", "category": "u793eu4f1au65b0u95fb", "num": 397012, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 397012, "note": "u897fu5b89u5168u5e02u6b63u5728u96c6u4e2du9694u79bb42000u4f59u4eba", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722331870299565", "onboard_time": 1641391188, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 23, "rank": 22}, {"label_name": "u70ed", "star_word": 0, "word": "u4ebau6c11u65e5u62a5u8bc4u897fu5b89u9632u75ab", "category": "u793eu4f1au65b0u95fb", "num": 386674, "subject_querys": "", "flag": 2, "icon_desc": "u70ed", "raw_hot": 386674, "note": "u4ebau6c11u65e5u62a5u8bc4u897fu5b89u9632u75ab", "emoticon": "", "is_hot": 1, "icon_desc_color": "#ff9406", "mid": "4722301847995845", "realpos": 24, "onboard_time": 1641371505, "ad_info": "", "topic_flag": 0, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 23}, {"label_name": "", "star_word": 0, "word": "u5f53u5357u65b9u72d7u5230u4e1cu5317u8fc7u5e74", "category": "u5e7du9ed8", "num": 375325, "subject_querys": "", "flag": 0, "onboard_time": 1641387524, "raw_hot": 375325, "note": "u5f53u5357u65b9u72d7u5230u4e1cu5317u8fc7u5e74", "emoticon": "", "ad_info": "", "mid": "4722368583306958", "realpos": 25, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 24}, {"label_name": "", "star_word": 0, "word": "u4e00u4e2au5c0fu602au7656u5f15u8d77u4e86u4e00u573au7231u60c5", "category": "u60c5u611f", "num": 357728, "subject_querys": "", "flag": 0, "onboard_time": 1641387945, "raw_hot": 357728, "note": "u4e00u4e2au5c0fu602au7656u5f15u8d77u4e86u4e00u573au7231u60c5", "emoticon": "", "ad_info": "", "mid": "4722360689102193", "realpos": 26, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 25}, {"label_name": "", "star_word": 0, "word": "u718au9edbu6797u597du50cfu5c0fu8bf4u91ccu7684u6076u6bd2u5c11u5976u5976", "category": "u5f71u89c6", "num": 336044, "subject_querys": "u5267u96c6|u5bb6u65cfu8363u8000", "flag": 0, "onboard_time": 1641378132, "raw_hot": 336044, "note": "u718au9edbu6797u597du50cfu5c0fu8bf4u91ccu7684u6076u6bd2u5c11u5976u5976", "emoticon": "", "ad_info": "", "mid": "4722346496360983", "flag_desc": "u5267u96c6", "realpos": 27, "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 26}, {"label_name": "", "star_word": 0, "word": "u738bu724cu90e8u961fu987eu4e00u91ceu51b3u5b9au7167u987eu963fu79c0", "category": "u5f71u89c6", "num": 331729, "subject_querys": "u5267u96c6|u738bu724cu90e8u961f", "flag": 0, "onboard_time": 1641379069, "raw_hot": 331729, "note": "u738bu724cu90e8u961fu987eu4e00u91ceu51b3u5b9au7167u987eu963fu79c0", "emoticon": "", "ad_info": "", "mid": "4722383221424691", "flag_desc": "u5267u96c6", "realpos": 28, "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 27}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5343u4e07u4e0du8981u5728u997fu7684u65f6u5019u770bu5c0fu8bf4", "category": "u7f8eu98df", "num": 290343, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 290343, "note": "u5343u4e07u4e0du8981u5728u997fu7684u65f6u5019u770bu5c0fu8bf4", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722377943943389", "onboard_time": 1641390285, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 29, "rank": 28}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u6d59u6c5fu6c38u5eb7u901au62a5u4e00u4f8bu9633u6027", "category": "u793eu4f1au65b0u95fb", "num": 289463, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 289463, "note": "u6d59u6c5fu6c38u5eb7u901au62a5u4e00u4f8bu9633u6027", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722393510054906", "onboard_time": 1641392873, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 30, "rank": 29}, {"label_name": "", "star_word": 0, "word": "u6768u5e42u767eu82b1u8fceu6625u665au4f1au4e0au73edu56fe", "category": "u7efcu827a", "num": 283388, "subject_querys": "u665au4f1a|u6587u8054u6625u665a", "flag": 0, "onboard_time": 1641382122, "raw_hot": 332051, "note": "u6768u5e42u767eu82b1u8fceu6625u665au4f1au4e0au73edu56fe", "emoticon": "", "ad_info": "", "mid": "4722378745054021", "realpos": 31, "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "", "rank": 30}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u674eu65afu4e39u59aeu4e00u4e2au4ebau5c31u662fu4e00u53f0u665au4f1a", "category": "u7efcu827a", "num": 282342, "realpos": 32, "flag": 1, "icon_desc": "u65b0", "raw_hot": 324363, "note": "u674eu65afu4e39u59aeu4e00u4e2au4ebau5c31u662fu4e00u53f0u665au4f1a", "emoticon": "", "subject_label": "u665au4f1a", "icon_desc_color": "#ff3852", "mid": "4722308364894860", "flag_desc": "u665au4f1a", "subject_querys": "u665au4f1a|u8292u679cu5e74u8d27u8282", "ad_info": "", "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "onboard_time": 1641389266, "rank": 31}, {"label_name": "", "star_word": 0, "word": "u8096u6218u987eu4e00u91ce", "category": "u5f71u89c6", "num": 281434, "subject_querys": "u5267u96c6|u738bu724cu90e8u961f", "flag": 0, "onboard_time": 1641383323, "raw_hot": 435332, "note": "u8096u6218u987eu4e00u91ce", "emoticon": "[u6cea]", "ad_info": "", "mid": "4722383221424691", "flag_desc": "u5267u96c6", "realpos": 33, "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 32}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u820du4e0du5f97u5206u4eabu7ed9u59d0u59b9u7684u5973u88c5u5e97", "category": "u65f6u5c1a", "num": 276061, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 276061, "note": "u820du4e0du5f97u5206u4eabu7ed9u59d0u59b9u7684u5973u88c5u5e97", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722376694563028", "onboard_time": 1641392748, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 34, "rank": 33}, {"label_name": "", "star_word": 0, "word": "u96eau4e2du608du5200u884c", "category": "u5f71u89c6", "num": 274052, "subject_querys": "u5267u96c6|u96eau4e2du608du5200u884c", "flag": 0, "onboard_time": 1641386080, "raw_hot": 274052, "note": "u96eau4e2du608du5200u884c", "emoticon": "", "ad_info": "", "mid": "4722213402447003", "flag_desc": "u5267u96c6", "realpos": 35, "topic_flag": 0, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 34}, {"label_name": "", "star_word": 0, "word": "u80e1u6b4cu8bf4u5b8cu7f8eu4f34u4fa3u6211u719fu554a", "category": "u5f71u89c6", "num": 269357, "subject_querys": "u5267u96c6|u5b8cu7f8eu4f34u4fa3", "flag": 0, "onboard_time": 1641384043, "raw_hot": 269357, "note": "u80e1u6b4cu8bf4u5b8cu7f8eu4f34u4fa3u6211u719fu554a", "emoticon": "", "ad_info": "", "mid": "4722328954997845", "flag_desc": "u5267u96c6", "realpos": 36, "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 35}, {"label_name": "u65b0", "is_new": 1, "star_word": 1, "word": "u5f20u96e8u7eeeu65f6u5c1au5148u751fu5e74u5ea6u7f8eu4e3du5973u4eba", "category": "u65f6u5c1a,u827au4eba", "num": 255086, "subject_querys": "u76dbu5178|u65f6u5c1au5148u751f", "flag": 1, "icon_desc": "u65b0", "raw_hot": 255086, "note": "u5f20u96e8u7eeeu65f6u5c1au5148u751fu5e74u5ea6u7f8eu4e3du5973u4eba", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722374177457960", "onboard_time": 1641392510, "ad_info": "", "topic_flag": 1, "channel_type": "Entertainment", "fun_word": 1, "realpos": 37, "rank": 36}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u793eu6050u7537u751fu4e0du6562u5f53u4f17u6f14u8bb2u5168u73edu5411u540eu8f6cu914du5408", "category": "u793eu4f1au65b0u95fb", "num": 255070, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 255070, "note": "u793eu6050u7537u751fu4e0du6562u5f53u4f17u6f14u8bb2u5168u73edu5411u540eu8f6cu914du5408", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722330276726230", "onboard_time": 1641392634, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 38, "rank": 37}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u8fd1u8dddu79bbu770bu9cb8u9c7cu53efu4ee5u6709u591au9707u64bc", "category": "u81eau7136", "num": 248269, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 248269, "note": "u8fd1u8dddu79bbu770bu9cb8u9c7cu53efu4ee5u6709u591au9707u64bc", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722373474127391", "onboard_time": 1641390348, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 39, "rank": 38}, {"label_name": "", "star_word": 0, "word": "u90d1u5ddeu672cu8f6eu75abu60c5u4e3bu8981u7531u5fb7u5c14u5854u6bd2u682au5f15u8d77", "category": "u793eu4f1au65b0u95fb", "num": 245249, "subject_querys": "", "flag": 0, "onboard_time": 1641387045, "raw_hot": 245249, "note": "u90d1u5ddeu672cu8f6eu75abu60c5u4e3bu8981u7531u5fb7u5c14u5854u6bd2u682au5f15u8d77", "emoticon": "", "ad_info": "", "mid": "4722353525230760", "realpos": 40, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 39}, {"label_name": "", "star_word": 0, "word": "u738bu724cu90e8u961f", "category": "u5f71u89c6", "num": 228755, "subject_querys": "u5267u96c6|u738bu724cu90e8u961f", "flag": 0, "onboard_time": 1641364185, "raw_hot": 228755, "note": "u738bu724cu90e8u961f", "emoticon": "[u6cea]", "ad_info": "", "mid": "4722349678789350", "flag_desc": "u5267u96c6", "realpos": 41, "topic_flag": 0, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 40}, {"label_name": "", "star_word": 0, "word": "u897fu5b89u75abu60c5u793eu4f1au9762u57fau672cu5b9eu73b0u6e05u96f6", "category": "u793eu4f1au65b0u95fb", "num": 220701, "subject_querys": "", "flag": 0, "onboard_time": 1641375161, "raw_hot": 220701, "note": "u897fu5b89u75abu60c5u793eu4f1au9762u57fau672cu5b9eu73b0u6e05u96f6", "emoticon": "", "ad_info": "", "mid": "4722326367633487", "realpos": 42, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 41}, {"label_name": "", "star_word": 0, "word": "u9ad8u5706u5706u6f14u7684u5f8bu5e08u9648u73ca", "category": "u5f71u89c6", "num": 198706, "subject_querys": "u5267u96c6|u5b8cu7f8eu4f34u4fa3", "flag": 0, "onboard_time": 1641381943, "raw_hot": 198706, "note": "u9ad8u5706u5706u6f14u7684u5f8bu5e08u9648u73ca", "emoticon": "", "ad_info": "", "mid": "4722371690759512", "flag_desc": "u5267u96c6", "realpos": 43, "topic_flag": 0, "channel_type": "Entertainment", "fun_word": 1, "subject_label": "u5267u96c6", "rank": 42}, {"label_name": "", "star_word": 0, "word": "iQOO9", "category": "u79d1u6280", "num": 196668, "subject_querys": "", "flag": 0, "onboard_time": 1641383565, "raw_hot": 196668, "note": "iQOO9", "emoticon": "", "ad_info": "", "mid": "4722383187871051", "realpos": 44, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 43}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u6d59u6c5fu91d1u534eu901au62a51u4f8bu9633u6027", "category": "u793eu4f1au65b0u95fb", "num": 190370, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 190370, "note": "u6d59u6c5fu91d1u534eu901au62a51u4f8bu9633u6027", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722394029623542", "onboard_time": 1641393235, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 45, "rank": 44}, {"label_name": "", "star_word": 0, "word": "u7f57u6c38u6d69u9650u5236u9ad8u6d88u8d39u4fe1u606fu6e05u96f6", "category": "u793eu4f1au65b0u95fb", "num": 183084, "subject_querys": "", "flag": 0, "onboard_time": 1641374383, "raw_hot": 183084, "note": "u7f57u6c38u6d69u9650u5236u9ad8u6d88u8d39u4fe1u606fu6e05u96f6", "emoticon": "", "ad_info": "", "mid": "4722338019149010", "realpos": 46, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 45}, {"label_name": "", "star_word": 0, "word": "u897fu5b89u4e00u623fu4e1cu7ed9u79dfu5ba2u514du4e00u4e2au6708u623fu79df", "category": "u793eu4f1au6b63u80fdu91cf", "num": 175798, "subject_querys": "", "flag": 0, "onboard_time": 1641381223, "raw_hot": 175798, "note": "u897fu5b89u4e00u623fu4e1cu7ed9u79dfu5ba2u514du4e00u4e2au6708u623fu79df", "emoticon": "", "ad_info": "", "mid": "4722319887173394", "realpos": 47, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 46}, {"label_name": "", "star_word": 0, "word": "u90d1u5ddeu7d2fu8ba17u4f8bu786eu8bca10u4f8bu65e0u75c7u72b6", "category": "u793eu4f1au65b0u95fb", "num": 164671, "subject_querys": "", "flag": 0, "onboard_time": 1641386205, "raw_hot": 164671, "note": "u90d1u5ddeu7d2fu8ba17u4f8bu786eu8bca10u4f8bu65e0u75c7u72b6", "emoticon": "", "ad_info": "", "mid": "4722370872871100", "realpos": 48, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 47}, {"label_name": "", "star_word": 0, "word": "u6d59u6c5fu7b2c100u573au75abu60c5u53d1u5e03u4f1a", "category": "u793eu4f1au65b0u95fb", "num": 143367, "subject_querys": "", "flag": 0, "onboard_time": 1641386864, "raw_hot": 143367, "note": "u6d59u6c5fu7b2c100u573au75abu60c5u53d1u5e03u4f1a", "emoticon": "", "ad_info": "", "mid": "4722369992592104", "realpos": 49, "topic_flag": 1, "channel_type": "", "fun_word": 0, "subject_label": "", "rank": 48}, {"label_name": "u65b0", "is_new": 1, "star_word": 0, "word": "u5f53u8defu4e0au591au4e86u4e00u4e2au8df3u623fu5b50u65f6", "category": "u5e7du9ed8", "num": 134672, "subject_querys": "", "flag": 1, "icon_desc": "u65b0", "raw_hot": 134672, "note": "u5f53u8defu4e0au591au4e86u4e00u4e2au8df3u623fu5b50u65f6", "emoticon": "", "subject_label": "", "icon_desc_color": "#ff3852", "mid": "4722388426558263", "onboard_time": 1641393534, "ad_info": "", "topic_flag": 1, "channel_type": "", "fun_word": 0, "realpos": 50, "rank": 49}]127.0.0.1 - - [05/Jan/2022 22:51:14] "GET /timelinechart/ HTTP/1.1" 200 -
*/

json遍历

下面的遍历优点在于不需要知道key具体是什么

代码语言:javascript复制
//原生js遍历JSON
 $.ajax({
        type: "GET",
        dataType: "json",
        url: "/data",
        //data: { id: id, name: name },
        success: function(json) {   
               
          for(var i=0;i<json.length;i  ){
            for(var key in json[i]){
              alert(key ':' json[i][key]);
            }
          }
 
        },
        error: function(json) {
          
        }
      });
代码语言:javascript复制
//jQuery遍历JSON

var json_1 = {"name":"jim","age":"28"};

$.each(json_1,function(key,value){   //其中key是json的key,value指相对应的值;

        console.log(key  " : "   value);

})

清洗

新手,hhh,我就把没必要的键值对扔了。

代码语言:javascript复制
 //清洗
          for(var i=0;i<json.length;i  ){
            delete json[i]['is_new'];
            delete json[i]['star_word'];
            delete json[i]['word'];
            delete json[i]['realpos'];
            delete json[i]['flag'];
            delete json[i]['raw_hot'];
            delete json[i]['emoticon'];
            delete json[i]['subject_label'];
            delete json[i]['icon_desc_color'];
            delete json[i]['flag_desc'];
            delete json[i]['subject_querys'];
            delete json[i]['ad_info'];
            delete json[i]['topic_flag'];
            delete json[i]['fun_word'];
            delete json[i]['icon_desc'];
            
          }

0 人点赞