首页 > 解决方案 > 如何将json结构存储为redis中的哈希数据结构

问题描述

我试图解决下面的 redis 示例。但我无法完成第 3 步。请有人帮助我。问题是:-

"city": "New Delhi",
"hotel": "Maidens Hotel",
"date": "2017-10-06",
"room_type": "single",
"occupancy": 2,
"avail_count": 13,
"booked_count": 4,
"currencyisocode": "INR",
"currency_name": "Rupee",
"provider": {
"name": "makemytrip",
"competitor_type": "partner"
},
"availroomnumbers": [ 3, 7, 23, 29, 41 ]
}

步骤1:-

考虑到上述 json 详细信息对于特定酒店是独一无二的 - 新德里市的少女酒店,日期为 2017 年 10 月 6 日,适用于单人房类型。这可以作为一个散列存储多个键,如 occupancy、avail_count、booked_count、currencyisocode 和 currencyname 以及各自的值。关键房费:New_Delhi:Maidens_Hotel:2017-10-06:single。根据需要将值传递给参数

编写一个命令来设置键 roomrate 下的上述值:New_Delhi:Maidens_Hotel:2017-10-06:single

答:-

HMSET roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single occupancy 2 avail_count 13 booked_count 4 currencyisocode INR currency_name Rupee

第 2 步:- 在另一个键 roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:provider 下将提供者添加为先前哈希中的另一个哈希,以提供与提供者相关的详细信息,例如名称和竞争者类型。根据数据提供的详细信息是

"provider": {
"name": "makemytrip",
"competitor_type": "partner"
}

在 key roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:provider 下编写一个命令来设置提供者的详细信息

答:-

HMSET roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:provider name makemytrip competitor_type partner

第3步: -

在键 roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:availroomnumbers 下的第一个哈希中添加availroomnumbers 作为一组。根据数据可用房间号为3、7、23、29和41

在 key roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:availroomnumbers 下编写命令设置可用房间号

暗示:-

SADD roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:availroomnumbers 3 7 29 41

标签: jsonredis

解决方案


尝试这个

HMSET roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single occupancy 2avail_count 13booked_count 4 currency_iso_code INR currency_name 卢比

HMSET roomrate:New_Delhi:Maidens_Hotel:2017-10-06:single:provider name makemytrip竞争对手_type合作伙伴

SADD 房费:New_Delhi:Maidens_Hotel:2017-10-06:single:availroomnumbers 3 7 23 29 41


推荐阅读