首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Javascript中访问字典中的值

如何在Javascript中访问字典中的值
EN

Stack Overflow用户
提问于 2020-06-18 18:31:38
回答 1查看 51关注 0票数 1

我正在尝试以下面的查询集形式从django中的item_id字段中提取model.py字段。

代码语言:javascript
复制
<QuerySet [<Item: Shoulder Bag Boys Shoulder Bag (Yellow )>,
<Item: Sweeter Cotton Sweeter>,
<Item: Shirt Full Sleeves Shirt>, <Item: Jacket Jackson Jacket>, 
<Item: Yellow Shoes Leopard Shoes>, 
<Item: Bag Mini Cary Bag>, <Item: Coat Overcoat (Gray)>,
<Item: TOWEL Pure Pineapple>,
<Item: Coat Pure Pineapple>, <Item: TOWEL Pure Pineapple (White)>]>

这是我的JS代码

代码语言:javascript
复制
 $.ajax({
            type: 'GET',
            url: '/shopsorting/' + selected_value,
            // data: formData,
            encode: true
        })
        .done(function(data) {
            items = JSON.parse(data)
            console.log(items)
            for (var item in items) {
                console.log(item['product_id'])
            };

但是它在控制台上打印

代码语言:javascript
复制
`(index):765
{items: "<QuerySet [<Item: TOWEL Pure Pineapple>, <Item: Ba…s Leopard Shoes>, <Item: Jacket Jackson 
Jacket>]>"}

(index):767 undefined`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-18 18:44:13

在接收数据之前尝试将数据转换为字符串:

代码语言:javascript
复制
x = JSON.stringify(data); 
items = JSON.parse(x);

别忘了

代码语言:javascript
复制
    $.ajax({
            type: 'GET',
            url: '\/shopsorting\/' + selected_value,
            // data: formData,
            encode: true,
            success: function(data) {
                x = JSON.stringify(data);
                items = JSON.parse(x);
                console.log(items);
                for (var item in items) {
                    console.log(item['product_id'])
                };
}});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62457046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档