在localstroge中查找项目值

时间:2017-08-21 11:10:22

标签: javascript java angularjs arrays

我有一个带有以下存储值的$ localstroge:

#region Namespaces
using System;
using System.Data;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using Microsoft.SqlServer.Dts.Runtime;
using System.Diagnostics;
using System.Collections.Generic;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types; 
#endregion

namespace ST_ef6fc7b20ff94cbfb2587ed23a6520a0
{

    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        public void Main()
        {
         OleDbDataAdapter A = new OleDbDataAdapter();
         System.Data.DataTable dt = new System.Data.DataTable();
         A.Fill(dt, Dts.Variables["User::Hosps"].Value);

        foreach (DataRow row in dt.Rows)
        {
             Tables.Add(row.ToString());
        }


          List<string> Tables = new List<string>();
             Tables.Add("ABRAVIV");
             Tables.Add("FLIMANAVIV");
             Tables.Add("BSHAVIV");
             Tables.Add("LEVAVIV");
             Tables.Add("BEERAVIV");
             Tables.Add("TIRAAVIV");
             Tables.Add("SHRAVIV");
             Tables.Add("MAZAVIV");
             Tables.Add("NTNYAAVIV");
             Tables.Add("SHMUELAVIV");
             Tables.Add("PARDESAVIV");
             Tables.Add("RISHONAVIV");

        Parallel.ForEach(Tables, Table =>
        {

             SqlConnection myConnection = new SqlConnection();
                          myConnection.ConnectionString = "Data Source=vmbi-devdb\\gp;Initial Catalog=GP;Integrated Security=True;Pooling=false;";
             SqlDataReader rdr = null;
             myConnection.Open();
             SqlCommand cmd = new SqlCommand("exec Mirror_Logistic.Load_Table_From_Source " + Table, myConnection);
             cmd.CommandTimeout = 0; 
             rdr = cmd.ExecuteReader();
             myConnection.Close();         


           });
            }
     #region ScriptResults declaration
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    }
}

如何从位于EmployerDetails内的身份获取Identity值。我已尝试过以下但如果条件:

它永远不会进入
{"EmployerDetails":{"Distance":30,"EmpLatitude":51.3353899,"EmpLongitude":-0.742856,"EmpNo":39424,"Insitution":null,"PlaceName":"Camberley","TalentPoolLicences":[{"Membership":[{"Identity":39424,"Name":"Weydon Secondary School"}],"TalentPoolType":1},{"Membership":[{"Identity":2,"Name":"North East Hampshire"},{"Identity":4,"Name":"Surrey"},{"Identity":8,"Name":"Surrey"}],"TalentPoolType":3}]},"FacetFilters":{"LastActivity":0,"LocationFilterType":1,"fullorparttime_pex":null,"religion":null,"soughtphase_swk":null,"soughtrole_swk":null,"soughtsubject_swk":null},"LookingFor":null,"OrderBy":null,"PageIndex":1,"PageSize":40}

对此有任何帮助吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试将其转换为json对象然后获取值?

答案 1 :(得分:0)

当您首先搜索嵌套密钥时,您需要抓取该对象,并且还需要使用JSON.parse将其解析为JSON,然后您可以像我们在普通javascript对象上一样继续

localStorage.getItem('signup-prefs')

这给了我一个包含我的对象的字符串

""name":"google","oauth_version":"2.0","oauth_server":"https://accounts.google.com/o/oauth2/auth","openid":"","username":""}"

解析后我们可以得到对象,现在我们可以找到所需的属性。

JSON.parse(localStorage.getItem('signup-prefs'))

Object {name: "google", oauth_version: "2.0", oauth_server: "https://accounts.google.com/o/oauth2/auth", openid: "", username: ""}

来你的问题

让我们说你的员工信息是这样的,我没有在这里显示所有字段。

var empData = {"EmployerDetails":Distance":30,"EmpLatitude":51.33538}}

然后你就像这样设置键

localstorage.setItem('empData', JSON.stringify(empData))

现在通过键将字符串对象解析为Json并从对象循环中找到所需的键以获得结果。我没有测试它但我相信它会起作用。如果没有,请告诉我。

for (var i = 0; i < localStorage.length; i++) {
     if (localStorage.key(i) === 'empData') {

        // Parse the string to json

         var empData = JSON.parse(localStorage.getItem('empData'));

        // get all the keys
         var keys = Object.keys(empData);

         for (var idx = 0; idx < keys.length; idx++) {
            // find the desired key here
             if (keys[idx] == 'EmployeeDetails') {
                 var empDetails = empData[keys[idx]]
             }
         }
     }
 }

您的代码的一个重要事项是 这个语句localStorage.key(i)) === 'EmployerDetails'返回true或false,并且这样写 if(localStorage.getItem(localStorage.key(i)) === 'EmployerDetails')永远不会被执行,因为你没有任何具有该名称的密钥(实际上我们绝不应该使用关键字作为密钥)。