首页 新闻 会员 周边 捐助

键重复的情况下,如何取出某个对象?或者如何取出相同键的一组?

0
悬赏园豆:5 [已解决问题] 解决于 2019-09-18 17:45
复制代码
var my_sys_roletujing_json={"1":"1102","1":"1103","1":"1104","1":"1105","1":"1107","1":"1108","1":"1111","1":"1112","1":"1113","1":"1114","1":"1115","1":"1116","1":"1119","1":"1120","1":"1123","1":"1125","1":"1126","1":"1127","1":"1128","1":"1129","1":"1130","1":"1131","1":"1135","1":"1136","1":"1138","1":"1139","1":"1154","1":"1155","1":"1156","1":"1159","1":"1160","1":"1161","1":"1162","1":"1163","1":"1164","1":"1165","1":"1166","1":"1172","1":"1173","1":"1175","1":"1176","1":"1177","1":"1180","3":"150","3":"151","3":"153","3":"154","3":"157","3":"158","3":"159","3":"160"};
复制代码
问题补充:

或许这样的格式不太正规,但已经是这样了不可逆,有没有解决方法?

大da脸的主页 大da脸 | 初学一级 | 园豆:73
提问于:2019-09-17 09:58

json转成list集合,然后通过linq查出相同键的数据不可以吗 比如list.where(p=>p.key==1)

默卿 5年前

@默卿: linq?你是说用lingq.js?

大da脸 5年前

@大da脸: 没有,我以为是后端。前端的话,你得到这个JSON的时候外层用单引号引起来变成字符串,然后去解析字符串吧,通过逗号分割成数组,再进一步通过:分割取值,存入数组。取的时候进行遍历,里面写if判断。

默卿 5年前

@默卿: 我就是这么弄的,但是我觉得应该有什么其他的方法

大da脸 5年前
< >
分享
最佳答案
0

这个是要js处理么?
不同语言不一样的。
如果按照你的这个js写:{1: "1180", 3: "160"},结果只是这个。
整理数据:

      var data_list=[];
    var my_sys_roletujing_json =
        '{"1":"1102","1":"1103","1":"1104","1":"1105","1":"1107","1":"1108","1":"1111","1":"1112","1":"1113","1":"1114","1":"1115","1":"1116","1":"1119","1":"1120","1":"1123","1":"1125","1":"1126","1":"1127","1":"1128","1":"1129","1":"1130","1":"1131","1":"1135","1":"1136","1":"1138","1":"1139","1":"1154","1":"1155","1":"1156","1":"1159","1":"1160","1":"1161","1":"1162","1":"1163","1":"1164","1":"1165","1":"1166","1":"1172","1":"1173","1":"1175","1":"1176","1":"1177","1":"1180","3":"150","3":"151","3":"153","3":"154","3":"157","3":"158","3":"159","3":"160"}';
    var myregexp = /"([0-9]*)":"([0-9]*)"/g;
    var match = myregexp.exec(my_sys_roletujing_json);
    var i=0
    while (match != null) {
        data_list.push({key:match[1],value:match[2]})
        match = myregexp.exec(my_sys_roletujing_json);
    }
    console.log(data_list)

1.如何取出某个对象
遍历列表,判断value=x

2.如何取出相同键的一组
遍历列表,判断key=x

收获园豆:5
张朋举 | 小虾三级 |园豆:1936 | 2019-09-17 15:49

非常感谢

大da脸 | 园豆:73 (初学一级) | 2019-09-18 17:44
其他回答(2)
0

自己解析字符串吧,如果没有合适的类库

会长 | 园豆:12463 (专家六级) | 2019-09-17 10:49
0
<script>
    var my_sys_roletujing_json='{"1":"1102","1":"1103","1":"1104","1":"1105","1":"1107","1":"1108","1":"1111","1":"1112","1":"1113","1":"1114","1":"1115","1":"1116","1":"1119","1":"1120","1":"1123","1":"1125","1":"1126","1":"1127","1":"1128","1":"1129","1":"1130","1":"1131","1":"1135","1":"1136","1":"1138","1":"1139","1":"1154","1":"1155","1":"1156","1":"1159","1":"1160","1":"1161","1":"1162","1":"1163","1":"1164","1":"1165","1":"1166","1":"1172","1":"1173","1":"1175","1":"1176","1":"1177","1":"1180","3":"150","3":"151","3":"153","3":"154","3":"157","3":"158","3":"159","3":"160"}';
    my_sys_roletujing_json_array = my_sys_roletujing_json.match(/"(\d+?)":"(\d+?)"/g)
    console.log(my_sys_roletujing_json_array)
        for (index in my_sys_roletujing_json_array) {
            console.log(my_sys_roletujing_json_array[index])
        key = my_sys_roletujing_json_array[index].match(/"(\d+?)":/)
        value = my_sys_roletujing_json_array[index].match(/:"(\d+?)"/)
        console.log('key:'+key[1])
        console.log('value:'+value[1])
    }
</script>

我觉得应该后台处理比较方便

import re
my_sys_roletujing_json = '{"1":"1102","1":"1103","1":"1104","1":"1105","1":"1107","1":"1108","1":"1111","1":"1112","1":"1113","1":"1114","1":"1115","1":"1116","1":"1119","1":"1120","1":"1123","1":"1125","1":"1126","1":"1127","1":"1128","1":"1129","1":"1130","1":"1131","1":"1135","1":"1136","1":"1138","1":"1139","1":"1154","1":"1155","1":"1156","1":"1159","1":"1160","1":"1161","1":"1162","1":"1163","1":"1164","1":"1165","1":"1166","1":"1172","1":"1173","1":"1175","1":"1176","1":"1177","1":"1180","3":"150","3":"151","3":"153","3":"154","3":"157","3":"158","3":"159","3":"160"}'
print(re.findall('"(\d+?)":"(\d+?)"',my_sys_roletujing_json))
#[('1', '1102'), ('1', '1103'), ('1', '1104'), ('1', '1105'), ('1', '1107'), ('1', '1108'), ('1', '1111'), ('1', '1112'), ('1', '1113'), ('1', '1114'), ('1', '1115'), ('1', '1116'), ('1', '1119'), ('1', '1120'), ('1', '1123'), ('1', '1125'), ('1', '1126'), ('1', '1127'), ('1', '1128'), ('1', '1129'), ('1', '1130'), ('1', '1131'), ('1', '1135'), ('1', '1136'), ('1', '1138'), ('1', '1139'), ('1', '1154'), ('1', '1155'), ('1', '1156'), ('1', '1159'), ('1', '1160'), ('1', '1161'), ('1', '1162'), ('1', '1163'), ('1', '1164'), ('1', '1165'), ('1', '1166'), ('1', '1172'), ('1', '1173'), ('1', '1175'), ('1', '1176'), ('1', '1177'), ('1', '1180'), ('3', '150'), ('3', '151'), ('3', '153'), ('3', '154'), ('3', '157'), ('3', '158'), ('3', '159'), ('3', '160')]
小小咸鱼YwY | 园豆:3312 (老鸟四级) | 2019-09-17 11:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册