/// <summary>
        /// Converts a JSON-file on string format to a list of corresponding NaceRegionData objects.
        /// </summary>
        /// <param name="jsonString">A JSON object represented as a string</param>
        /// <param name="attributeName">The name of the datafield. Used to find and set the correct property in NaceRegionObjects</param>
        /// <returns></returns>
        public async Task <List <NaceRegionData> > Convert(string jsonString, string attributeName)
        {
            un = new EuroStatJSONUnNester(jsonString);
            List <NaceRegionData> nrdList = new List <NaceRegionData>();
            List <String>         naceRegionYearFields  = new List <String>();
            List <int>            numberOfItemsInFields = new List <int>();


            if (!un.IsValidDataset())
            {
                return(nrdList);
            }

            List <Region> regions = await databaseStore.getAllRegions();

            List <Nace> naces = await databaseStore.getAllNaces();


            foreach (KeyValuePair <String, String> entry in un.GetValues())
            {
                List <int> totalElementsList = un.GetFields().ConvertAll(field => field.totalElements);
                List <int> indexes           = findIndexesOfFields(int.Parse(entry.Key), totalElementsList);

                int naceId   = indexes[un.GetFieldIndex("nace")];
                int regionId = indexes[un.GetFieldIndex("region")];
                int yearId   = indexes[un.GetFieldIndex("year")];

                string naceCode   = un.GetNaceCode(naceId: naceId);
                string regionCode = un.GetRegionCode(regionId: regionId);
                int    year       = un.GetYear(yearId: yearId);

                double propValue = double.Parse(entry.Value, CultureInfo.InvariantCulture);

                Nace   nace   = naces.Find(nace => nace.naceCode == naceCode);
                Region region = regions.Find(region => region.regionCode == regionCode);

                if (nace != null && region != null)
                {
                    NaceRegionData nrd = new NaceRegionData()
                    {
                        naceId = nace.naceId, regionId = region.regionId, year = year
                    };
                    Type         type = nrd.GetType();
                    PropertyInfo prop = type.GetProperty(attributeName);
                    prop.SetValue(nrd, propValue, null);
                    nrdList.Add(nrd);
                }
            }
            return(nrdList);
        }
コード例 #2
0
 public void GetNaceShouldFindCorrectRegionForJsonUnNester2(int regionId, string expected)
 {
     Assert.AreEqual(expected, jun1.GetRegionCode(regionId));
 }