无法将对象数组存储到本地存储中(Angular 9)

时间:2020-07-18 15:51:21

标签: json angular typescript local-storage angular9

我正在尝试将json对象数组存储到本地存储中,但我不知道为什么它不起作用。我尝试使用localStorage.setItem('comparisons', JSON.stringify(setComparisons)),但由于某种原因没有存储任何内容。

我知道我要存储的阵列具有我要存储的信息,因为当我在存储之前在控制台中将其记录在控制台中时,这些信息就全部存在了。

我还将类似的对象(虽然不是上述对象的数组)存储在localStorage中,并且可以毫无问题地检索它们。我不知道是否是因为这是一个嵌套有数组数组的数组或其他内容,但是我无法弄清楚。

这是我的代码:

import { Component, OnDestroy, OnInit, ElementRef } from '@angular/core';

import * as jasmineData from '../../../../assets/data/jasmine.json';
import * as belleData from '../../../../assets/data/belle.json';
import * as trumpData from '../../../../assets/data/trump.json';
import * as katyPerryData from '../../../../assets/data/katyPerry.json';
import * as snoopDogData from '../../../../assets/data/snoopDog.json';

@Component({
  selector: 'ngx-header',
  styleUrls: ['./header.component.scss'],
  templateUrl: './header.component.html',
})


export class HeaderComponent implements OnInit, OnDestroy {
 
 constructor(){}
 
 ngOnInit(){

    if(localStorage.getItem('comparisons')){

      console.log("localStorage 'comparisons', set to: " + JSON.parse(localStorage.getItem('comparisons')))

      let testOutput = JSON.parse(localStorage.getItem('comparisons')) 

      console.log(testOutput) //<<<<< outputs nothing

    }
    else{

      let setComparisons = []

      setComparisons['jasmine'] = jasmineData['default']
      setComparisons['belle'] = belleData['default']
      setComparisons['trump'] = trumpData['default']
      setComparisons['katyPerry'] = katyPerryData['default']
      setComparisons['snoopDog'] = snoopDogData['default']

      console.log(setComparisons) //<<<<<<<<<< this works
 
      localStorage.setItem('comparisons', JSON.stringify(setComparisons)) //<<<<<<<<<<<< this doesn't work

    }
 }

这是数组内json对象之一的示例:

trumpData['default'] = 

{
  "word_count": 43336,
  "processed_language": "en",
  "personality": [
    {
      "trait_id": "big5_openness",
      "name": "Openness",
      "category": "personality",
      "percentile": 0.9635579519385504,
      "raw_score": 0.8059142660098279,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_adventurousness",
          "name": "Adventurousness",
          "category": "personality",
          "percentile": 0.6272749945581317,
          "raw_score": 0.5163639722247335,
          "significant": true
        },
        {
          "trait_id": "facet_artistic_interests",
          "name": "Artistic interests",
          "category": "personality",
          "percentile": 0.5549904649909926,
          "raw_score": 0.672324948928981,
          "significant": true
        },
        {
          "trait_id": "facet_emotionality",
          "name": "Emotionality",
          "category": "personality",
          "percentile": 0.28058970637230307,
          "raw_score": 0.6245047073901818,
          "significant": true
        },
        {
          "trait_id": "facet_imagination",
          "name": "Imagination",
          "category": "personality",
          "percentile": 0.19081218603999944,
          "raw_score": 0.6914017394955871,
          "significant": true
        },
        {
          "trait_id": "facet_intellect",
          "name": "Intellect",
          "category": "personality",
          "percentile": 0.9812082240575732,
          "raw_score": 0.7034946040858857,
          "significant": true
        },
        {
          "trait_id": "facet_liberalism",
          "name": "Authority-challenging",
          "category": "personality",
          "percentile": 0.61502043736225,
          "raw_score": 0.5309578314898162,
          "significant": true
        }
      ]
    },
    {
      "trait_id": "big5_conscientiousness",
      "name": "Conscientiousness",
      "category": "personality",
      "percentile": 0.983679161419448,
      "raw_score": 0.727697468159296,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_achievement_striving",
          "name": "Achievement striving",
          "category": "personality",
          "percentile": 0.9037639682132805,
          "raw_score": 0.7574010167811566,
          "significant": true
        },
        {
          "trait_id": "facet_cautiousness",
          "name": "Cautiousness",
          "category": "personality",
          "percentile": 0.9577606864915309,
          "raw_score": 0.6013821376687818,
          "significant": true
        },
        {
          "trait_id": "facet_dutifulness",
          "name": "Dutifulness",
          "category": "personality",
          "percentile": 0.8973055247237618,
          "raw_score": 0.6899127477824422,
          "significant": true
        },
        {
          "trait_id": "facet_orderliness",
          "name": "Orderliness",
          "category": "personality",
          "percentile": 0.6418796567676541,
          "raw_score": 0.5060623294980398,
          "significant": true
        },
        {
          "trait_id": "facet_self_discipline",
          "name": "Self-discipline",
          "category": "personality",
          "percentile": 0.9263490077860836,
          "raw_score": 0.6396844920960025,
          "significant": true
        },
        {
          "trait_id": "facet_self_efficacy",
          "name": "Self-efficacy",
          "category": "personality",
          "percentile": 0.8527803075035358,
          "raw_score": 0.7899712120456678,
          "significant": true
        }
      ]
    },
    {
      "trait_id": "big5_extraversion",
      "name": "Extraversion",
      "category": "personality",
      "percentile": 0.881805735940201,
      "raw_score": 0.6038432240030011,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_activity_level",
          "name": "Activity level",
          "category": "personality",
          "percentile": 0.6792742431580725,
          "raw_score": 0.564207098178294,
          "significant": true
        },
        {
          "trait_id": "facet_assertiveness",
          "name": "Assertiveness",
          "category": "personality",
          "percentile": 0.9590138650026484,
          "raw_score": 0.7310095956169131,
          "significant": true
        },
        {
          "trait_id": "facet_cheerfulness",
          "name": "Cheerfulness",
          "cttegory": "personality",
          "percentile": 0.22893261753027844,
          "raw_score": 0.5932251409353175,
          "significant": true
        },
        {
          "trait_id": "facet_excitement_seeking",
          "name": "Excitement-seeking",
          "category": "personality",
          "percentile": 0.07232250370319887,
          "raw_score": 0.5417548838864124,
          "significant": true
        },
        {
          "trait_id": "facet_friendliness",
          "name": "Outgoing",
          "category": "personality",
          "percentile": 0.6581074192508122,
          "raw_score": 0.5809171726499361,
          "significant": true
        },
        {
          "trait_id": "facet_gregariousness",
          "name": "Gregariousness",
          "category": "personality",
          "percentile": 0.20363538067938197,
          "raw_score": 0.4107392559079223,
          "significant": true
        }
      ]
    },
    {
      "trait_id": "big5_agreeableness",
      "name": "Agreeableness",
      "category": "personality",
      "percentile": 0.636750368115894,
      "raw_score": 0.7561040242628326,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_altruism",
          "name": "Altruism",
          "category": "personality",
          "percentile": 0.8106883789426762,
          "raw_score": 0.7359758754762658,
          "significant": true
        },
        {
          "trait_id": "facet_cooperation",
          "name": "Cooperation",
          "category": "personality",
          "percentile": 0.5694503320384123,
          "raw_score": 0.5899309252875584,
          "significant": true
        },
        {
          "trait_id": "facet_modesty",
          "name": "Modesty",
          "category": "personality",
          "percentile": 0.40713913944183266,
          "raw_score": 0.43598028092279484,
          "significant": true
        },
        {
          "trait_id": "facet_morality",
          "name": "Uncompromising",
          "category": "personality",
          "percentile": 0.8195717940201414,
          "raw_score": 0.6689246296236344,
          "significant": true
        },
        {
          "trait_id": "facet_sympathy",
          "name": "Sympathy",
          "category": "personality",
          "percentile": 0.9920577260424773,
          "raw_score": 0.755562118052558,
          "significant": true
        },
        {
          "trait_id": "facet_trust",
          "name": "Trust",
          "category": "personality",
          "percentile": 0.47298674696745124,
          "raw_score": 0.5830101087027785,
          "significant": true
        }
      ]
    },
    {
      "trait_id": "big5_neuroticism",
      "name": "Emotional range",
      "category": "personality",
      "percentile": 0.13904938204251915,
      "raw_score": 0.4595539073395768,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_anger",
          "name": "Fiery",
          "category": "personality",
          "percentile": 0.14592765984036238,
          "raw_score": 0.47807350135271154,
          "significant": true
        },
        {
          "trait_id": "facet_anxiety",
          "name": "Prone to worry",
          "category": "personality",
          "percentile": 0.08697282793529115,
          "raw_score": 0.5013887984737312,
          "significant": true
        },
        {
          "trait_id": "facet_depression",
          "name": "Melancholy",
          "category": "personality",
          "percentile": 0.16680824488529322,
          "raw_score": 0.3872001388285126,
          "significant": true
        },
        {
          "trait_id": "facet_immoderation",
          "name": "Immoderation",
          "category": "personality",
          "percentile": 0.05246097936296118,
          "raw_score": 0.4382727933313163,
          "significant": true
        },
        {
          "trait_id": "facet_self_consciousness",
          "name": "Self-consciousness",
          "category": "personality",
          "percentile": 0.07969774599416085,
          "raw_score": 0.47267337369288276,
          "significant": true
        },
        {
          "trait_id": "facet_vulnerability",
          "name": "Susceptible to stress",
          "category": "personality",
          "percentile": 0.0273807153901362,
          "raw_score": 0.3452475153303576,
          "significant": true
        }
      ]
    }
  ],
  "needs": [
    {
      "trait_id": "need_challenge",
      "name": "Challenge",
      "category": "needs",
      "percentile": 0.08110205321079733,
      "raw_score": 0.669099706309445,
      "significant": true
    },
    {
      "trait_id": "need_closeness",
      "name": "Closeness",
      "category": "needs",
      "percentile": 0.09291402531402237,
      "raw_score": 0.7346366531130437,
      "significant": true
    },
    {
      "trait_id": "need_curiosity",
      "name": "Curiosity",
      "category": "needs",
      "percentile": 0.2183155687091271,
      "raw_score": 0.7961768121584263,
      "significant": true
    },
    {
      "trait_id": "need_excitement",
      "name": "Excitement",
      "category": "needs",
      "percentile": 0.02286176344029084,
      "raw_score": 0.5264979907029215,
      "significant": true
    },
    {
      "trait_id": "need_harmony",
      "name": "Harmony",
      "category": "needs",
      "percentile": 0.03427014251932459,
      "raw_score": 0.7463631682958176,
      "significant": true
    },
    {
      "trait_id": "need_ideal",
      "name": "Ideal",
      "category": "needs",
      "percentile": 0.05741159343084268,
      "raw_score": 0.6161180176812197,
      "significant": true
    },
    {
      "trait_id": "need_liberty",
      "name": "Liberty",
      "category": "needs",
      "percentile": 0.02216408649103313,
      "raw_score": 0.6611766254368321,
      "significant": true
    },
    {
      "trait_id": "need_love",
      "name": "Love",
      "category": "needs",
      "percentile": 0.015052473849888615,
      "raw_score": 0.6631615559158821,
      "significant": true
    },
    {
      "trait_id": "need_practicality",
      "name": "Practicality",
      "category": "needs",
      "percentile": 0.08611576821290123,
      "raw_score": 0.6982802364811073,
      "significant": true
    },
    {
      "trait_id": "need_self_expression",
      "name": "Self-expression",
      "category": "needs",
      "percentile": 0.07504383162546596,
      "raw_score": 0.6253916730109863,
      "significant": true
    },
    {
      "trait_id": "need_stability",
      "name": "Stability",
      "category": "needs",
      "percentile": 0.41330474153377117,
      "raw_score": 0.727472877321253,
      "significant": true
    },
    {
      "trait_id": "need_structure",
      "name": "Structure",
      "category": "needs",
      "percentile": 0.5785073814374321,
      "raw_score": 0.7014458089713079,
      "significant": true
    }
  ],
  "values": [
    {
      "trait_id": "value_conservation",
      "name": "Conservation",
      "category": "values",
      "percentile": 0.16678415814865472,
      "raw_score": 0.6218884680694995,
      "significant": true
    },
    {
      "trait_id": "value_openness_to_change",
      "name": "Openness to change",
      "category": "values",
      "percentile": 0.08647094085994467,
      "raw_score": 0.7317164337859383,
      "significant": true
    },
    {
      "trait_id": "value_hedonism",
      "name": "Hedonism",
      "category": "values",
      "percentile": 0.047762743756957216,
      "raw_score": 0.6316746177041674,
      "significant": true
    },
    {
      "trait_id": "value_self_enhancement",
      "name": "Self-enhancement",
      "category": "values",
      "percentile": 0.00590828237726021,
      "raw_score": 0.5958789067897858,
      "significant": true
    },
    {
      "trait_id": "value_self_transcendence",
      "name": "Self-transcendence",
      "category": "values",
      "percentile": 0.05270413599836593,
      "raw_score": 0.8065859855129559,
      "significant": true
    }
  ],
  "consumption_preferences": [
    {
      "consumption_preference_category_id": "consumption_preferences_shopping",
      "name": "Purchasing Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_automobile_ownership_cost",
          "name": "Likely to be sensitive to ownership cost when buying automobiles",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_automobile_safety",
          "name": "Likely to prefer safety when buying automobiles",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_clothes_quality",
          "name": "Likely to prefer quality when buying clothes",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_clothes_style",
          "name": "Likely to prefer style when buying clothes",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_clothes_comfort",
          "name": "Likely to prefer comfort when buying clothes",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_influence_brand_name",
          "name": "Likely to be influenced by brand name when making product purchases",
          "score": 0.5
        },
        {
          "consumption_preference_id": "consumption_preferences_influence_utility",
          "name": "Likely to be influenced by product utility when making product purchases",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_influence_online_ads",
          "name": "Likely to be influenced by online ads when making product purchases",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_influence_social_media",
          "name": "Likely to be influenced by social media when making product purchases",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_influence_family_members",
          "name": "Likely to be influenced by family when making product purchases",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_spur_of_moment",
          "name": "Likely to indulge in spur of the moment purchases",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_credit_card_payment",
          "name": "Likely to prefer using credit cards for shopping",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_health_and_activity",
      "name": "Health & Activity Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_eat_out",
          "name": "Likely to eat out frequently",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_gym_membership",
          "name": "Likely to have a gym membership",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_outdoor",
          "name": "Likely to like outdoor activities",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_environmental_concern",
      "name": "Environmental Concern Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_concerned_environment",
          "name": "Likely to be concerned about the environment",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_entrepreneurship",
      "name": "Entrepreneurship Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_start_business",
          "name": "Likely to consider starting a business in next few years",
          "score": 0.5
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_movie",
      "name": "Movie Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_movie_romance",
          "name": "Likely to like romance movies",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_adventure",
          "name": "Likely to like adventure movies",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_horror",
          "name": "Likely to like horror movies",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_musical",
          "name": "Likely to like musical movies",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_historical",
          "name": "Likely to like historical movies",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_science_fiction",
          "name": "Likely to like science-fiction movies",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_war",
          "name": "Likely to like war movies",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_drama",
          "name": "Likely to like drama movies",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_action",
          "name": "Likely to like action movies",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_movie_documentary",
          "name": "Likely to like documentary movies",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_music",
      "name": "Music Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_music_rap",
          "name": "Likely to like rap music",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_country",
          "name": "Likely to like country music",
          "score": 0.5
        },
        {
          "consumption_preference_id": "consumption_preferences_music_r_b",
          "name": "Likely to like R&B music",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_hip_hop",
          "name": "Likely to like hip hop music",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_live_event",
          "name": "Likely to attend live musical events",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_playing",
          "name": "Likely to have experience playing music",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_latin",
          "name": "Likely to like Latin music",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_music_rock",
          "name": "Likely to like rock music",
          "score": 0.5
        },
        {
          "consumption_preference_id": "consumption_preferences_music_classical",
          "name": "Likely to like classical music",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_reading",
      "name": "Reading Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_read_frequency",
          "name": "Likely to read often",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_books_entertainment_magazines",
          "name": "Likely to read entertainment magazines",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_books_non_fiction",
          "name": "Likely to read non-fiction books",
          "score": 1.0
        },
        {
          "consumption_preference_id": "consumption_preferences_books_financial_investing",
          "name": "Likely to read financial investment books",
          "score": 0.0
        },
        {
          "consumption_preference_id": "consumption_preferences_books_autobiographies",
          "name": "Likely to read autobiographical books",
          "score": 1.0
        }
      ]
    },
    {
      "consumption_preference_category_id": "consumption_preferences_volunteering",
      "name": "Volunteering Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_volunteer",
          "name": "Likely to volunteer for social causes",
          "score": 1.0
        }
      ]
    }
  ],
  "warnings": [
    {
      "warning_id": "CONTENT_TRUNCATED",
      "message": "For maximum accuracy while also optimizing processing time, only the first 250KB of input text (excluding markup) was analyzed. Accuracy levels off at approximately 3,000 words so this did not affect the accuracy of the profile."
    }
  ]
}

谢谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您正在初始化数组,但随后使用字符串键设置数据。因此,要么使用实际对象:

const setComparisons = {
  jasmine: jasmineData['default'],
  // etc
};

或使用具有数字索引的数组:

const setComparisons = [
  jasmineData['default'],
  // etc
];

您可以将setDatagetData保持原样。

之所以不起作用,是因为数组上的本机toJSON方法仅考虑了数组中的项目,而不考虑对象上可能存在的任何其他属性。在对对象进行字符串化时调用此方法