新手问题 如何将转换成字符串的对象,重新转换成对象?

xiajian · 2016年03月18日 · 最后由 yakjuly 回复于 2016年03月23日 · 3794 次阅读

如何将转换成字符串的对象,重新转换成对象。具体的就是,将 hash 对象存放到 redis 之后,再取出来,恢复成一个 hash 对象。

比如,我有一个 hash 对象 a1:

[102] pry(main)> a123
=> {"aeopPostCategoryList"=>
  [{"id"=>3,
    "level"=>0,
    "names"=>{"zh"=>"服装/服饰配件", "pt"=>"Vestuário e acessórios", "fr"=>"Vêtements & accessoires", "en"=>"Apparel & Accessories", "ru"=>"Одежда и аксессуары", "es"=>"Ropa y accesorios", "in"=>"Pakaian & aksesoris"},
    "isleaf"=>false},
   {"id"=>34,
    "level"=>0,
    "names"=>{"zh"=>"汽车、摩托车", "pt"=>"Automóveis e motos", "fr"=>"Automobiles et motos", "en"=>"Automobiles & Motorcycles", "ru"=>"Автомобили и мотоциклы", "es"=>"Automóviles y motocicletas", "in"=>"Mobil & sepeda motor"},
    "isleaf"=>false},
   {"id"=>66, "level"=>0, "names"=>{"zh"=>"美容健康", "pt"=>"Beleza e saúde", "fr"=>"Beauté & santé", "en"=>"Beauty & Health", "ru"=>"Красоты и здоровья", "es"=>"Belleza y salud", "in"=>"Kecantikan & kesehatan"}, "isleaf"=>false},
   {"id"=>200004360,
    "level"=>0,
    "names"=>{"zh"=>"俄罗斯当地图书销售", "pt"=>"Livros locais para russo", "fr"=>"Livres pour locale russe", "en"=>"Books for Local Russian", "ru"=>"Книги для местных русский", "es"=>"Libros para Local ruso", "in"=>"Buku untuk lokal Russian"},
    "isleaf"=>false},
   {"id"=>7,
    "level"=>0,
    "names"=>{"zh"=>"电脑和办公", "pt"=>"Computador e escritório", "fr"=>"Ordinateur et bureau", "en"=>"Computer & Office", "ru"=>"Компьютер и офиса", "es"=>"Ordenador y oficina", "in"=>"Komputer & kantor"},
    "isleaf"=>false},
   {"id"=>13,
    "level"=>0,
    "names"=>
     {"zh"=>"建筑", "pt"=>"Construção & Real Estate", "fr"=>"Immobilier & Construction", "en"=>"Construction & Real Estate", "ru"=>"Строительство и недвижимость", "es"=>"Construcción y bienes raíces", "in"=>"Konstruksi & Real Estate"},
    "isleaf"=>false},
   {"id"=>44,
    "level"=>0,
    "names"=>{"zh"=>"消费电子", "pt"=>"Eletrônicos de consumo", "fr"=>"Consumer Electronics", "en"=>"Consumer Electronics", "ru"=>"Бытовая электроника", "es"=>"Electrónica de consumo", "in"=>"Elektronik konsumen"},
    "isleaf"=>false},
   {"id"=>100008578,
    "level"=>0,
    "names"=>{"zh"=>"定制化产品", "pt"=>"Produtos personalizados", "fr"=>"Produits personnalisés", "en"=>"Customized Products", "ru"=>"Настроенные продукты", "es"=>"Productos personalizados", "in"=>"Produk disesuaikan"},
    "isleaf"=>false},
   {"id"=>5,
    "level"=>0,
    "names"=>
     {"zh"=>"电气设备和产品",
      "pt"=>"Equipamentos eléctricos e suprimentos",
      "fr"=>"Electrical Equipment & Supplies",
      "en"=>"Electrical Equipment & Supplies",
      "ru"=>"Электротехника и дополнения",
      "es"=>"Equipo eléctrico y suministros",
      "in"=>"Peralatan listrik & perlengkapan"},
    "isleaf"=>false},

其存放到 redis 中,再取出来就变成这样了:

"{\"aeopPostCategoryList\"=>[{\"id\"=>3, \"level\"=>0, \"names\"=>{\"zh\"=>\"服装/服饰配件\", \"pt\"=>\"Vestuário e acessórios\", \"fr\"=>\"Vêtements & accessoires\", \"en\"=>\"Apparel & Accessories\", \"ru\"=>\"Одежда и аксессуары\", \"es\"=>\"Ropa y accesorios\", \"in\"=>\"Pakaian & aksesoris\"}, \"isleaf\"=>false}, {\"id\"=>34, \"level\"=>0, \"names\"=>{\"zh\"=>\"汽车、摩托车\", \"pt\"=>\"Automóveis e motos\", \"fr\"=>\"Automobiles et motos\", \"en\"=>\"Automobiles & Motorcycles\", \"ru\"=>\"Автомобили и мотоциклы\", \"es\"=>\"Automóviles y motocicletas\", \"in\"=>\"Mobil & sepeda motor\"}, \"isleaf\"=>false}, {\"id\"=>66, \"level\"=>0, \"names\"=>{\"zh\"=>\"美容健康\", \"pt\"=>\"Beleza e saúde\", \"fr\"=>\"Beauté & santé\", \"en\"=>\"Beauty & Health\", \"ru\"=>\"Красоты и здоровья\", \"es\"=>\"Belleza y salud\", \"in\"=>\"Kecantikan & kesehatan\"}, \"isleaf\"=>false}, {\"id\"=>200004360, \"level\"=>0, \"names\"=>{\"zh\"=>\"俄罗斯当地图书销售\", \"pt\"=>\"Livros locais para russo\", \"fr\"=>\"Livres pour locale russe\", \"en\"=>\"Books for Local Russian\", \"ru\"=>\"Книги для местных русский\", \"es\"=>\"Libros para Local ruso\", \"in\"=>\"Buku untuk lokal Russian\"}, \"isleaf\"=>false}, {\"id\"=>7, \"level\"=>0, \"names\"=>{\"zh\"=>\"电脑和办公\", \"pt\"=>\"Computador e escritório\", \"fr\"=>\"Ordinateur et bureau\", \"en\"=>\"Computer & Office\", \"ru\"=>\"Компьютер и офиса\", \"es\"=>\"Ordenador y oficina\", \"in\"=>\"Komputer & kantor\"}, \"isleaf\"=>false}, {\"id\"=>13, \"level\"=>0, \"names\"=>{\"zh\"=>\"建筑\", \"pt\"=>\"Construção & Real Estate\", \"fr\"=>\"Immobilier & Construction\", \"en\"=>\"Construction & Real Estate\", \"ru\"=>\"Строительство и недвижимость\", \"es\"=>\"Construcción y bienes raíces\", \"in\"=>\"Konstruksi & Real Estate\"}, \"isleaf\"=>false}, {\"id\"=>44, \"level\"=>0, \"names\"=>{\"zh\"=>\"消费电子\", \"pt\"=>\"Eletrônicos de consumo\", \"fr\"=>\"Consumer Electronics\", \"en\"=>\"Consumer Electronics\", \"ru\"=>\"Бытовая электроника\", \"es\"=>\"Electrónica de consumo\", \"in\"=>\"Elektronik konsumen\"}, \"isleaf\"=>false}, {\"id\"=>100008578, \"level\"=>0, \"names\"=>{\"zh\"=>\"定制化产品\", \"pt\"=>\"Produtos personalizados\", \"fr\"=>\"Produits personnalisés\", \"en\"=>\"Customized Products\", \"ru\"=>\"Настроенные продукты\", \"es\"=>\"Productos personalizados\", \"in\"=>\"Produk disesuaikan\"}, \"isleaf\"=>false}, {\"id\"=>5, \"level\"=>0, \"names\"=>{\"zh\"=>\"电气设备和产品\", \"pt\"=>\"Equipamentos eléctricos e suprimentos\", \"fr\"=>\"Electrical Equipment & Supplies\", \"en\"=>\"Electrical Equipment & Supplies\", \"ru\"=>\"Электротехника и дополнения\", \"es\"=>\"Equipo eléctrico y suministros\", \"in\"=>\"Peralatan listrik & perlengkapan\"}, \"isleaf\"=>false}, {\"id\"=>502, \"level\"=>0, \"names\"=>{\"zh\"=>\"电子元器件\", \"pt\"=>\"Componentes e sistemas electrónicas\", \"fr\"=>\"Composants électroniques et les\", \"en\"=>\"Electronic Components & Supplies\", \"ru\"=>\"Электронных компонентов и расходных материалов\", \"es\"=>\"Componentes y sistemas electrónicos\", \"in\"=>\"Komponen elektronik & perlengkapan\"}, \"isleaf\"=>false}, {\"id\"=>2, \"level\"=>0, \"names\"=>{\"zh\"=>\"食品\", \"pt\"=>\"Alimentos\", \"fr\"=>\"Alimentaire\", \"en\"=>\"Food\", \"ru\"=>\"Питание\", \"es\"=>\"Alimentos\", \"in\"=>\"Makanan\"}, \"isleaf\"=>false}, {\"id\"=>1503, \"level\"=>0, \"names\"=>{\"zh\"=>\"家具和室内装饰品\", \"pt\"=>\"Móveis\", \"fr\"=>\"Meubles\", \"en\"=>\"Furniture\", \"ru\"=>\"Мебель\", \"es\"=>\"Muebles\", \"in\"=>\"Furniture\"}, \"isleaf\"=>false}, {\"id\"=>200003655, \"level\"=>0, \"names\"=>{\"zh\"=>\"假发及周边\", \"pt\"=>\"Cabelo e acessórios\", \"fr\"=>\"Cheveux et accessoires\", \"en\"=>\"Hair & Accessories\", \"ru\"=>\"Волосы и аксессуары\", \"es\"=>\"Pelo y accesorios\", \"in\"=>\"Rambut & aksesoris\"}, \"isleaf\"=>false}, {\"id\"=>42, \"level\"=>0, \"names\"=>{\"zh\"=>\"五金\", \"pt\"=>\"Hardware\", \"fr\"=>\"Matériel\", \"en\"=>\"Hardware\", \"ru\"=>\"Оборудование\", \"es\"=>\"Hardware\", \"in\"=>\"Hardware\"}, \"isleaf\"=>false}, {\"id\"=>15, \"level\"=>0, \"names\"=>{\"zh\"=>\"家居用品\", \"pt\"=>\"Casa e jardim\", \"fr\"=>\"Maison & jardin\", \"en\"=>\"Home & Garden\", \"ru\"=>\"Дома и сада\", \"es\"=>\"Hogar y jardín\", \"in\"=>\"Home & Garden\"}, \"isleaf\"=>false}, {\"id\"=>6, \"level\"=>0, \"names\"=>{\"zh\"=>\"家用电器\", \"pt\"=>\"Eletrodomésticos\", \"fr\"=>\"Appareils ménagers\", \"en\"=>\"Home Appliances\", \"ru\"=>\"Бытовая техника\", \"es\"=>\"Electrodomésticos\", \"in\"=>\"Peralatan rumah\"}, \"isleaf\"=>false}, {\"id\"=>200001996, \"level\"=>0, \"names\"=>{\"zh\"=>\"工业/原材料/商业用品\", \"pt\"=>\"Indústria e negócios\", \"fr\"=>\"Industrie et d'affaires\", \"en\"=>\"Industry & Business\", \"ru\"=>\"Промышленности и бизнес\", \"es\"=>\"Industria y negocio\", \"in\"=>\"Industri & bisnis\"}, \"isleaf\"=>false}, {\"id\"=>36, \"level\"=>0, \"names\"=>{\"zh\"=>\"珠宝手表\", \"pt\"=>\"Jóias & relógios\", \"fr\"=>\"Bijoux et montre\", \"en\"=>\"Jewelry & Watch\", \"ru\"=>\"Ювелирные изделия и часы\", \"es\"=>\"Joyería y reloj\", \"in\"=>\"Perhiasan & menonton\"}, \"isleaf\"=>false}, {\"id\"=>39, \"level\"=>0, \"names\"=>{\"zh\"=>\"照明灯饰\", \"pt\"=>\"Luzes\", \"fr\"=>\"Lumières et éclairage\", \"en\"=>\"Lights & Lighting\", \"ru\"=>\"Огни и освещение\", \"es\"=>\"Luces e iluminación\", \"in\"=>\"Lampu & pencahayaan\"}, \"isleaf\"=>false}, {\"id\"=>1524, \"level\"=>0, \"names\"=>{\"zh\"=>\"箱包\", \"pt\"=>\"Bagagem & Bags\", \"fr\"=>\"Bagages et sacs\", \"en\"=>\"Luggage & Bags\", \"ru\"=>\"Багажа и сумки\", \"es\"=>\"Equipaje y bolsas\", \"in\"=>\"Bagasi & tas\"}, \"isleaf\"=>false}, {\"id\"=>1501, \"level\"=>0, \"names\"=>{\"zh\"=>\"孕婴童\", \"pt\"=>\"Mãe & Kids\", \"fr\"=>\"Mère et enfants\", \"en\"=>\"Mother & Kids\", \"ru\"=>\"Мать и дети\", \"es\"=>\"La madre y de los niños\", \"in\"=>\"Ibu & anak-anak\"}, \"isleaf\"=>false}, {\"id\"=>21, \"level\"=>0, \"names\"=>{\"zh\"=>\"办公文教用品\", \"pt\"=>\"Escritório e material escolar\", \"fr\"=>\"Bureau et fournitures scolaires\", \"en\"=>\"Office & School Supplies\", \"ru\"=>\"Офисные и школьные принадлежности\", \"es\"=>\"Office & School Supplies\", \"in\"=>\"Office & School Supplies\"}, \"isleaf\"=>false}, {\"id\"=>509, \"level\"=>0, \"names\"=>{\"zh\"=>\"电话和通讯\", \"pt\"=>\"Telefones e telecomunicações\", \"fr\"=>\"Téléphones et télécommunications\", \"en\"=>\"Phones & Telecommunications\", \"ru\"=>\"Телефоны и телекоммуникации\", \"es\"=>\"Teléfonos y telecomunicaciones\", \"in\"=>\"Ponsel & telekomunikasi\"}, \"isleaf\"=>false}, {\"id\"=>30, \"level\"=>0, \"names\"=>{\"zh\"=>\"安全防护\", \"pt\"=>\"Segurança e proteção\", \"fr\"=>\"Sécurité et Protection\", \"en\"=>\"Security & Protection\", \"ru\"=>\"Безопасности и защиты\", \"es\"=>\"Seguridad y protección\", \"in\"=>\"Keamanan & perlindungan\"}, \"isleaf\"=>false}, {\"id\"=>322, \"level\"=>0, \"names\"=>{\"zh\"=>\"鞋子\", \"pt\"=>\"Sapatos\", \"fr\"=>\"Chaussures\", \"en\"=>\"Shoes\", \"ru\"=>\"Обувь\", \"es\"=>\"Zapatos\", \"in\"=>\"Sepatu\"}, \"isleaf\"=>false}, {\"id\"=>200001075, \"level\"=>0, \"names\"=>{\"zh\"=>\"其他特殊类\", \"pt\"=>\"Categoria especial\", \"fr\"=>\"Catégorie spéciale\", \"en\"=>\"Special Category\", \"ru\"=>\"Специальная категория\", \"es\"=>\"Especial categoría\", \"in\"=>\"Kategori khusus\"}, \"isleaf\"=>false}, {\"id\"=>18, \"level\"=>0, \"names\"=>{\"zh\"=>\"运动及娱乐\", \"pt\"=>\"Esporte e lazer\", \"fr\"=>\"Sports & Entertainment\", \"en\"=>\"Sports & Entertainment\", \"ru\"=>\"Спорт и развлечения\", \"es\"=>\"Deportes y entretenimiento\", \"in\"=>\"Olahraga & hiburan\"}, \"isleaf\"=>false}, {\"id\"=>1420, \"level\"=>0, \"names\"=>{\"zh\"=>\"工具\", \"pt\"=>\"Ferramentas\", \"fr\"=>\"Outils\", \"en\"=>\"Tools\", \"ru\"=>\"Инструменты\", \"es\"=>\"Herramientas\", \"in\"=>\"Alat\"}, \"isleaf\"=>false}, {\"id\"=>26, \"level\"=>0, \"names\"=>{\"zh\"=>\"玩具\", \"pt\"=>\"Brinquedos e Hobbies\", \"fr\"=>\"Jouets & loisirs\", \"en\"=>\"Toys & Hobbies\", \"ru\"=>\"Игрушки и хобби\", \"es\"=>\"Juguetes y pasatiempos\", \"in\"=>\"Mainan & hobi\"}, \"isleaf\"=>false}, {\"id\"=>200003498, \"level\"=>0, \"names\"=>{\"zh\"=>\"旅游及代金券\", \"pt\"=>\"Viagens e serviços de cupom\", \"fr\"=>\"Voyage promo Services\", \"en\"=>\"Travel and Coupon Services\", \"ru\"=>\"Путешествия и купон услуги\", \"es\"=>\"Viajes y cupón servicios\", \"in\"=>\"Perjalanan dan jasa kupon\"}, \"isleaf\"=>false}, {\"id\"=>320, \"level\"=>0, \"names\"=>{\"zh\"=>\"婚礼及重要场合\", \"pt\"=>\"Casamentos e eventos\", \"fr\"=>\"Mariages et événements\", \"en\"=>\"Weddings & Events\", \"ru\"=>\"Свадьбы и события\", \"es\"=>\"Bodas y eventos\", \"in\"=>\"Pernikahan & acara\"}, \"isleaf\"=>false}], \"success\"=>true}"

使用,JSON.parse 貌似不行。

从 redis 取出来的数据像是 a123.to_s 的结果。试试 a123.to_json 之后再存到 redis

#1 楼 @zhengpd to_json 之后,又转义了一层。

=> "\"{\\\"aeopPostCategoryList\\\"=>[{\\\"id\\\"=>3, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"服装/服饰配件\\\", \\\"pt\\\"=>\\\"Vestuário e acessórios\\\", \\\"fr\\\"=>\\\"Vêtements & accessoires\\\", \\\"en\\\"=>\\\"Apparel & Accessories\\\", \\\"ru\\\"=>\\\"Одежда и аксессуары\\\", \\\"es\\\"=>\\\"Ropa y accesorios\\\", \\\"in\\\"=>\\\"Pakaian & aksesoris\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>34, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"汽车、摩托车\\\", \\\"pt\\\"=>\\\"Automóveis e motos\\\", \\\"fr\\\"=>\\\"Automobiles et motos\\\", \\\"en\\\"=>\\\"Automobiles & Motorcycles\\\", \\\"ru\\\"=>\\\"Автомобили и мотоциклы\\\", \\\"es\\\"=>\\\"Automóviles y motocicletas\\\", \\\"in\\\"=>\\\"Mobil & sepeda motor\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>66, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"美容健康\\\", \\\"pt\\\"=>\\\"Beleza e saúde\\\", \\\"fr\\\"=>\\\"Beauté & santé\\\", \\\"en\\\"=>\\\"Beauty & Health\\\", \\\"ru\\\"=>\\\"Красоты и здоровья\\\", \\\"es\\\"=>\\\"Belleza y salud\\\", \\\"in\\\"=>\\\"Kecantikan & kesehatan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>200004360, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"俄罗斯当地图书销售\\\", \\\"pt\\\"=>\\\"Livros locais para russo\\\", \\\"fr\\\"=>\\\"Livres pour locale russe\\\", \\\"en\\\"=>\\\"Books for Local Russian\\\", \\\"ru\\\"=>\\\"Книги для местных русский\\\", \\\"es\\\"=>\\\"Libros para Local ruso\\\", \\\"in\\\"=>\\\"Buku untuk lokal Russian\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>7, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"电脑和办公\\\", \\\"pt\\\"=>\\\"Computador e escritório\\\", \\\"fr\\\"=>\\\"Ordinateur et bureau\\\", \\\"en\\\"=>\\\"Computer & Office\\\", \\\"ru\\\"=>\\\"Компьютер и офиса\\\", \\\"es\\\"=>\\\"Ordenador y oficina\\\", \\\"in\\\"=>\\\"Komputer & kantor\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>13, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"建筑\\\", \\\"pt\\\"=>\\\"Construção & Real Estate\\\", \\\"fr\\\"=>\\\"Immobilier & Construction\\\", \\\"en\\\"=>\\\"Construction & Real Estate\\\", \\\"ru\\\"=>\\\"Строительство и недвижимость\\\", \\\"es\\\"=>\\\"Construcción y bienes raíces\\\", \\\"in\\\"=>\\\"Konstruksi & Real Estate\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>44, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"消费电子\\\", \\\"pt\\\"=>\\\"Eletrônicos de consumo\\\", \\\"fr\\\"=>\\\"Consumer Electronics\\\", \\\"en\\\"=>\\\"Consumer Electronics\\\", \\\"ru\\\"=>\\\"Бытовая электроника\\\", \\\"es\\\"=>\\\"Electrónica de consumo\\\", \\\"in\\\"=>\\\"Elektronik konsumen\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>100008578, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"定制化产品\\\", \\\"pt\\\"=>\\\"Produtos personalizados\\\", \\\"fr\\\"=>\\\"Produits personnalisés\\\", \\\"en\\\"=>\\\"Customized Products\\\", \\\"ru\\\"=>\\\"Настроенные продукты\\\", \\\"es\\\"=>\\\"Productos personalizados\\\", \\\"in\\\"=>\\\"Produk disesuaikan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>5, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"电气设备和产品\\\", \\\"pt\\\"=>\\\"Equipamentos eléctricos e suprimentos\\\", \\\"fr\\\"=>\\\"Electrical Equipment & Supplies\\\", \\\"en\\\"=>\\\"Electrical Equipment & Supplies\\\", \\\"ru\\\"=>\\\"Электротехника и дополнения\\\", \\\"es\\\"=>\\\"Equipo eléctrico y suministros\\\", \\\"in\\\"=>\\\"Peralatan listrik & perlengkapan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>502, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"电子元器件\\\", \\\"pt\\\"=>\\\"Componentes e sistemas electrónicas\\\", \\\"fr\\\"=>\\\"Composants électroniques et les\\\", \\\"en\\\"=>\\\"Electronic Components & Supplies\\\", \\\"ru\\\"=>\\\"Электронных компонентов и расходных материалов\\\", \\\"es\\\"=>\\\"Componentes y sistemas electrónicos\\\", \\\"in\\\"=>\\\"Komponen elektronik & perlengkapan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>2, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"食品\\\", \\\"pt\\\"=>\\\"Alimentos\\\", \\\"fr\\\"=>\\\"Alimentaire\\\", \\\"en\\\"=>\\\"Food\\\", \\\"ru\\\"=>\\\"Питание\\\", \\\"es\\\"=>\\\"Alimentos\\\", \\\"in\\\"=>\\\"Makanan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>1503, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"家具和室内装饰品\\\", \\\"pt\\\"=>\\\"Móveis\\\", \\\"fr\\\"=>\\\"Meubles\\\", \\\"en\\\"=>\\\"Furniture\\\", \\\"ru\\\"=>\\\"Мебель\\\", \\\"es\\\"=>\\\"Muebles\\\", \\\"in\\\"=>\\\"Furniture\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>200003655, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"假发及周边\\\", \\\"pt\\\"=>\\\"Cabelo e acessórios\\\", \\\"fr\\\"=>\\\"Cheveux et accessoires\\\", \\\"en\\\"=>\\\"Hair & Accessories\\\", \\\"ru\\\"=>\\\"Волосы и аксессуары\\\", \\\"es\\\"=>\\\"Pelo y accesorios\\\", \\\"in\\\"=>\\\"Rambut & aksesoris\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>42, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"五金\\\", \\\"pt\\\"=>\\\"Hardware\\\", \\\"fr\\\"=>\\\"Matériel\\\", \\\"en\\\"=>\\\"Hardware\\\", \\\"ru\\\"=>\\\"Оборудование\\\", \\\"es\\\"=>\\\"Hardware\\\", \\\"in\\\"=>\\\"Hardware\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>15, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"家居用品\\\", \\\"pt\\\"=>\\\"Casa e jardim\\\", \\\"fr\\\"=>\\\"Maison & jardin\\\", \\\"en\\\"=>\\\"Home & Garden\\\", \\\"ru\\\"=>\\\"Дома и сада\\\", \\\"es\\\"=>\\\"Hogar y jardín\\\", \\\"in\\\"=>\\\"Home & Garden\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>6, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"家用电器\\\", \\\"pt\\\"=>\\\"Eletrodomésticos\\\", \\\"fr\\\"=>\\\"Appareils ménagers\\\", \\\"en\\\"=>\\\"Home Appliances\\\", \\\"ru\\\"=>\\\"Бытовая техника\\\", \\\"es\\\"=>\\\"Electrodomésticos\\\", \\\"in\\\"=>\\\"Peralatan rumah\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>200001996, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"工业/原材料/商业用品\\\", \\\"pt\\\"=>\\\"Indústria e negócios\\\", \\\"fr\\\"=>\\\"Industrie et d'affaires\\\", \\\"en\\\"=>\\\"Industry & Business\\\", \\\"ru\\\"=>\\\"Промышленности и бизнес\\\", \\\"es\\\"=>\\\"Industria y negocio\\\", \\\"in\\\"=>\\\"Industri & bisnis\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>36, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"珠宝手表\\\", \\\"pt\\\"=>\\\"Jóias & relógios\\\", \\\"fr\\\"=>\\\"Bijoux et montre\\\", \\\"en\\\"=>\\\"Jewelry & Watch\\\", \\\"ru\\\"=>\\\"Ювелирные изделия и часы\\\", \\\"es\\\"=>\\\"Joyería y reloj\\\", \\\"in\\\"=>\\\"Perhiasan & menonton\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>39, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"照明灯饰\\\", \\\"pt\\\"=>\\\"Luzes\\\", \\\"fr\\\"=>\\\"Lumières et éclairage\\\", \\\"en\\\"=>\\\"Lights & Lighting\\\", \\\"ru\\\"=>\\\"Огни и освещение\\\", \\\"es\\\"=>\\\"Luces e iluminación\\\", \\\"in\\\"=>\\\"Lampu & pencahayaan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>1524, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"箱包\\\", \\\"pt\\\"=>\\\"Bagagem & Bags\\\", \\\"fr\\\"=>\\\"Bagages et sacs\\\", \\\"en\\\"=>\\\"Luggage & Bags\\\", \\\"ru\\\"=>\\\"Багажа и сумки\\\", \\\"es\\\"=>\\\"Equipaje y bolsas\\\", \\\"in\\\"=>\\\"Bagasi & tas\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>1501, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"孕婴童\\\", \\\"pt\\\"=>\\\"Mãe & Kids\\\", \\\"fr\\\"=>\\\"Mère et enfants\\\", \\\"en\\\"=>\\\"Mother & Kids\\\", \\\"ru\\\"=>\\\"Мать и дети\\\", \\\"es\\\"=>\\\"La madre y de los niños\\\", \\\"in\\\"=>\\\"Ibu & anak-anak\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>21, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"办公文教用品\\\", \\\"pt\\\"=>\\\"Escritório e material escolar\\\", \\\"fr\\\"=>\\\"Bureau et fournitures scolaires\\\", \\\"en\\\"=>\\\"Office & School Supplies\\\", \\\"ru\\\"=>\\\"Офисные и школьные принадлежности\\\", \\\"es\\\"=>\\\"Office & School Supplies\\\", \\\"in\\\"=>\\\"Office & School Supplies\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>509, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"电话和通讯\\\", \\\"pt\\\"=>\\\"Telefones e telecomunicações\\\", \\\"fr\\\"=>\\\"Téléphones et télécommunications\\\", \\\"en\\\"=>\\\"Phones & Telecommunications\\\", \\\"ru\\\"=>\\\"Телефоны и телекоммуникации\\\", \\\"es\\\"=>\\\"Teléfonos y telecomunicaciones\\\", \\\"in\\\"=>\\\"Ponsel & telekomunikasi\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>30, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"安全防护\\\", \\\"pt\\\"=>\\\"Segurança e proteção\\\", \\\"fr\\\"=>\\\"Sécurité et Protection\\\", \\\"en\\\"=>\\\"Security & Protection\\\", \\\"ru\\\"=>\\\"Безопасности и защиты\\\", \\\"es\\\"=>\\\"Seguridad y protección\\\", \\\"in\\\"=>\\\"Keamanan & perlindungan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>322, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"鞋子\\\", \\\"pt\\\"=>\\\"Sapatos\\\", \\\"fr\\\"=>\\\"Chaussures\\\", \\\"en\\\"=>\\\"Shoes\\\", \\\"ru\\\"=>\\\"Обувь\\\", \\\"es\\\"=>\\\"Zapatos\\\", \\\"in\\\"=>\\\"Sepatu\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>200001075, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"其他特殊类\\\", \\\"pt\\\"=>\\\"Categoria especial\\\", \\\"fr\\\"=>\\\"Catégorie spéciale\\\", \\\"en\\\"=>\\\"Special Category\\\", \\\"ru\\\"=>\\\"Специальная категория\\\", \\\"es\\\"=>\\\"Especial categoría\\\", \\\"in\\\"=>\\\"Kategori khusus\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>18, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"运动及娱乐\\\", \\\"pt\\\"=>\\\"Esporte e lazer\\\", \\\"fr\\\"=>\\\"Sports & Entertainment\\\", \\\"en\\\"=>\\\"Sports & Entertainment\\\", \\\"ru\\\"=>\\\"Спорт и развлечения\\\", \\\"es\\\"=>\\\"Deportes y entretenimiento\\\", \\\"in\\\"=>\\\"Olahraga & hiburan\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>1420, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"工具\\\", \\\"pt\\\"=>\\\"Ferramentas\\\", \\\"fr\\\"=>\\\"Outils\\\", \\\"en\\\"=>\\\"Tools\\\", \\\"ru\\\"=>\\\"Инструменты\\\", \\\"es\\\"=>\\\"Herramientas\\\", \\\"in\\\"=>\\\"Alat\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>26, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"玩具\\\", \\\"pt\\\"=>\\\"Brinquedos e Hobbies\\\", \\\"fr\\\"=>\\\"Jouets & loisirs\\\", \\\"en\\\"=>\\\"Toys & Hobbies\\\", \\\"ru\\\"=>\\\"Игрушки и хобби\\\", \\\"es\\\"=>\\\"Juguetes y pasatiempos\\\", \\\"in\\\"=>\\\"Mainan & hobi\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>200003498, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"旅游及代金券\\\", \\\"pt\\\"=>\\\"Viagens e serviços de cupom\\\", \\\"fr\\\"=>\\\"Voyage promo Services\\\", \\\"en\\\"=>\\\"Travel and Coupon Services\\\", \\\"ru\\\"=>\\\"Путешествия и купон услуги\\\", \\\"es\\\"=>\\\"Viajes y cupón servicios\\\", \\\"in\\\"=>\\\"Perjalanan dan jasa kupon\\\"}, \\\"isleaf\\\"=>false}, {\\\"id\\\"=>320, \\\"level\\\"=>0, \\\"names\\\"=>{\\\"zh\\\"=>\\\"婚礼及重要场合\\\", \\\"pt\\\"=>\\\"Casamentos e eventos\\\", \\\"fr\\\"=>\\\"Mariages et événements\\\", \\\"en\\\"=>\\\"Weddings & Events\\\", \\\"ru\\\"=>\\\"Свадьбы и события\\\", \\\"es\\\"=>\\\"Bodas y eventos\\\", \\\"in\\\"=>\\\"Pernikahan & acara\\\"}, \\\"isleaf\\\"=>false}], \\\"success\\\"=>true}\""

#1 楼 @zhengpd 你的意思是,to_json 之后再存,然后,使用 JSON.parse 解析。我试试。

想到一个方法:Marshal.dump 对象,然后存放到 redis 中,然后,读取时是用 Marshal.load 将对象恢复出来。

同遇到过这个问题,最后还是存成了 json 用的时候再解析。Redis 是 key-value database,原则上是应该可以给任何 language 使用,不仅于 ruby

对于这类我觉得 yaml 可以用。 存入 YAML.dump(目标), 取出后 YAML.load(目标).

edit: 还是用 Marshal 吧,yaml 比较之下慢一点。

你是怎么存成 string 的,就用相反的方法转换回来。

你刚从 redis 拿出来的 直接 JSON.parse

#10 楼 @nevermore328 你这么用然后会发现某天整个服务器都被 rm 了

#7 楼 @yakjuly 存 Redis 中的,默认调用的to_s转化的。

#8 楼 @u1440247613 解析报错啊,不然也不会 这么纠结:

JSON.parse a123
JSON::ParserError: 757: unexpected token at '{"attributes"=>[{"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007782, "values"=>[{"id"=>124, "names"=>{"zh"=>"塑料", "en"=>"Plastic"}, "attributes"=>[{"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007784, "values"=>[{"id"=>439, "names"=>{"zh"=>"PVC", "en"=>"PVC"}, "attributes"=>[]}, {"id"=>445, "names"=>{"zh"=>"PET", "en"=>"PET"}, "attributes"=>[]}, {"id"=>352537, "names"=>{"zh"=>"HDPE", "en"=>"HDPE"}, "attributes"=>[]}, {"id"=>501, "names"=>{"zh"=>"PC", "en"=>"PC"}, "attributes"=>[]}, {"id"=>438, "names"=>{"zh"=>"PP", "en"=>"PP"}, "attributes"=>[]}, {"id"=>437, "names"=>{"zh"=>"PE", "en"=>"PE"}, "attributes"=>[]}, {"id"=>4, "names"=>{"zh"=>"其它", "en"=>"Other"}, "attributes"=>[]}], "names"=>{"zh"=>"塑料类别", "en"=>"Plastic Type"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"list_box"}]}, {"id"=>346, "names"=>{"zh"=>"玻璃", "en"=>"Glass"}, "attributes"=>[]}, {"id"=>747, "names"=>{"zh"=>"陶瓷", "en"=>"Ceramic"}, "attributes"=>[]}, {"id"=>365507, "names"=>{"zh"=>"粘土", "en"=>"Clay"}, "attributes"=>[]}, {"id"=>2455, "names"=>{"zh"=>"铝合金", "en"=>"Aluminium Alloy"}, "attributes"=>[]}, {"id"=>206, "names"=>{"zh"=>"不锈钢", "en"=>"Stainless Steel"}, "attributes"=>[]}, {"id"=>350382, "names"=>{"zh"=>"树脂", "en"=>"Acrylic"}, "attributes"=>[]}, {"id"=>398, "names"=>{"zh"=>"金属", "en"=>"Metal"}, "attributes"=>[]}, {"id"=>4, "names"=>{"zh"=>"其它", "en"=>"Other"}, "attributes"=>[]}], "names"=>{"zh"=>"促销品材质", "en"=>"Material"}, "inputType"=>"STRING", "required"=>true, "attributeShowTypeValue"=>"list_box"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007728, "values"=>[{"id"=>100018793, "names"=>{"zh"=>"右手握住时可见", "en"=>"Right Hand Readable"}, "attributes"=>[]}, {"id"=>100018794, "names"=>{"zh"=>"左手握住时可见", "en"=>"Left Hand Readable"}, "attributes"=>[]}, {"id"=>100018795, "names"=>{"zh"=>"双手握住均可见", "en"=>"Both Hand Readable"}, "attributes"=>[]}, {"id"=>100018796, "names"=>{"zh"=>"杯底", "en"=>"Bottom Surface"}, "attributes"=>[]}, {"id"=>4, "names"=>{"zh"=>"其它", "en"=>"Other"}, "attributes"=>[]}], "names"=>{"zh"=>"印制区域", "en"=>"Imprint Place"}, "inputType"=>"STRING", "required"=>true, "attributeShowTypeValue"=>"check_box"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007781, "values"=>[{"id"=>100018897, "names"=>{"zh"=>"常规产品", "en"=>"Normal Product"}, "attributes"=>[]}, {"id"=>100018898, "names"=>{"zh"=>"新奇特产品", "en"=>"Novetly Product"}, "attributes"=>[{"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007783, "values"=>[{"id"=>397, "names"=>{"zh"=>"LED", "en"=>"LED"}, "attributes"=>[]}, {"id"=>1648, "names"=>{"zh"=>"变色", "en"=>"Color Changing"}, "attributes"=>[]}, {"id"=>100018788, "names"=>{"zh"=>"夜光", "en"=>"Glow in the Dark"}, "attributes"=>[]}, {"id"=>100018894, "names"=>{"zh"=>"可写粉笔字", "en"=>"Chalk Writen"}, "attributes"=>[]}, {"id"=>100018805, "names"=>{"zh"=>"发声", "en"=>"Music"}, "attributes"=>[]}, {"id"=>100018806, "names"=>{"zh"=>"带USB接口的", "en"=>"USB-Enabled"}, "attributes"=>[]}, {"id"=>4, "names"=>{"zh"=>"其它", "en"=>"Other"}, "attributes"=>[]}], "names"=>{"zh"=>"新奇特样式", "en"=>"Novelty"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"check_box"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007758, "values"=>[{"id"=>100018852, "names"=>{"zh"=>"新设计", "en"=>"New Design"}, "attributes"=>[]}, {"id"=>100018853, "names"=>{"zh"=>"新功能", "en"=>"New Function"}, "attributes"=>[]}, {"id"=>100018854, "names"=>{"zh"=>"新材质", "en"=>"New Material"}, "attributes"=>[]}, {"id"=>100018855, "names"=>{"zh"=>"新造型", "en"=>"New Style"}, "attributes"=>[]}, {"id"=>100018856, "names"=>{"zh"=>"新工艺", "en"=>"New Technology"}, "attributes"=>[]}, {"id"=>100018857, "names"=>{"zh"=>"新专利", "en"=>"New Patent"}, "attributes"=>[]}], "names"=>{"zh"=>"新奇特产品类型", "en"=>"Special Features"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"check_box"}]}], "names"=>{"zh"=>"促销品类别", "en"=>"Kind"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"list_box"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007785, "values"=>[{"id"=>100018808, "names"=>{"zh"=>"双层杯壁", "en"=>"Double Wall"}, "attributes"=>[]}, {"id"=>100018809, "names"=>{"zh"=>"单层杯壁", "en"=>"Single Wall"}, "attributes"=>[]}, {"id"=>100018810, "names"=>{"zh"=>"带杯柄", "en"=>"With Handle"}, "attributes"=>[]}, {"id"=>100018811, "names"=>{"zh"=>"无杯柄", "en"=>"Without Handle"}, "attributes"=>[]}, {"id"=>4, "names"=>{"zh"=>"其它", "en"=>"Other"}, "attributes"=>[]}], "names"=>{"zh"=>"产品样式", "en"=>"Style"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"check_box"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007739, "names"=>{"zh"=>"产品颜色", "en"=>"Product Color"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"input"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007787, "names"=>{"zh"=>"产品容量", "en"=>"Volume"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"input"}, {"spec"=>0, "visible"=>true, "customizedName"=>false, "customizedPic"=>false, "keyAttribute"=>false, "sku"=>false, "id"=>100007763, "names"=>{"zh"=>"杯口直径", "en"=>"Diameter"}, "inputType"=>"STRING", "required"=>false, "attributeShowTypeValue"=>"input"}], "success"=>true}'
from /Users/xiajian/.rvm/gems/ruby-2.1.5@aliexpress/gems/json-1.8.3/lib/json/common.rb:155:in `parse'

#10 楼 @nevermore328 确实是个办法,谢谢了。

#12 楼 @xiajian 用 to_json 或者 ActiveSupport::JSON.encode 方法存,用 ActiveSupport::JSON.decode 取

#15 楼 @yakjuly

ActiveSupport 的 json 解析挺慢的:

Here's what parsing a 2.43MB JSON file off the filesystem 20 times looks like: Memory Usage Average

Yajl::Parser#parse: 32MB JSON.parse: 54MB ActiveSupport::JSON.decode: 63MB

Peak

Yajl::Parser#parse: 32MB JSON.parse: 57MB ActiveSupport::JSON.decode: 67MB

Parse Time

Yajl::Parser#parse: 4.54s JSON.parse: 5.47s ActiveSupport::JSON.decode: 64.42s

Encode Time

Yajl::Encoder#encode: 3.59s JSON#to_json: 6.2s ActiveSupport::JSON.encode: 45.58s

@xiajian 如果内容这么长都几十兆了,你要考虑下你要拿这么多内容做什么。我觉得这个设计问题就很大。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号