首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >搜索具有键值对象的数组的数组

搜索具有键值对象的数组的数组
EN

Stack Overflow用户
提问于 2019-04-16 07:08:47
回答 4查看 67关注 0票数 -1

我正在使用一个API,它返回JSON,如下所示:

    {
        "lookup_table_data": [
            [
                {
                    "key": "id",
                    "value": 0
                },
                {
                    "key" : "label",
                    "value" : "something"
                }
            ],
            [
                {
                    "key": "id",
                    "value": 1
                },
                {
                    "key" : "label",
                    "value" : "something_else"
                }
            ]
       ]
  }

我创建了一个类,将json对象反序列化为如下所示:

public class LookupResponseModel
    {
        public Lookup_Table_Data[][] Lookup_table_data { get; set; }

        public class Lookup_Table_Data
        {
            public string Key { get; set; }
            public object Value { get; set; }
        }
    }      

现在假设JSON响应有超过1000条记录,而不是我在示例中给出的2条记录。

我想要搜索我的模型,并能够找到键"id“的值等于1的位置-因为我想使用标签键值"something_else”。

我怎样才能用这个模型来获取id为1的标签"something_else“呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-04-16 07:58:55

var lookup = model.lookupTableData.Select(data => new { key = (long)data.First(kvp => kvp.Key == "id").Value, value = (string)data.First(kvp => kvp.Key == "label").Value).ToDictionary(kvp => kvp.key, kvp => kvp.value)
var displayText = lookup[1]; // "something_else"

我在手机上的尝试,可能不是100%正确的语法。

票数 1
EN

Stack Overflow用户

发布于 2019-04-16 07:51:35

我会建议这样一种方法:

public class LookupResponseModel
{
    public Lookup_Table_Data[][] Lookup_table_data { get; set; }

    public class Lookup_Table_Data
    {
        public string Key { get; set; }
        public object Value { get; set; }
    }
}

// This will help compare the values and convert if necessary
// This part was missing from my original answer and made it not work
bool ObjectCompare(object a, object b)
{
    if (object.Equals(a, b))
    {
        return true;
    }
    else
    {
        var altB = Convert.ChangeType(b, Type.GetTypeCode(a.GetType()));
        return object.Equals(a, altB);
    }
}

// This will break the result up into an Array of Dictionaries
// that are easier to work with
Dictionary<string, object>[] MakeTables(LookupResponseModel lrm)
{
    return lrm.Lookup_table_data.Select( entry => entry.ToDictionary( e => e.Key, e => e.Value ) ).ToArray();
}

// This will help you find the dictionary that has the values you want
Dictionary<string, object> FindTable( Dictionary<string, object>[] tables, string key, object value )
{
    return tables.Where( dict => dict.TryGetValue(key, out object val) && ObjectCompare(value, val) ).FirstOrDefault(); 
}

// Here is how you might use them together
string GetLabel()
{
    var lrm = JsonConvert.DeserializeObject<LookupResponseModel>(json);
    var lookup = MakeTables(lrm);

    var table = FindTable( lookup, "id", 1 );

    return table["label"].ToString();  // Returns "something_else"
}
票数 1
EN

Stack Overflow用户

发布于 2019-04-16 07:25:17

简单的答案是,你说这样的话

public class LookupResponseModel
{
  public LookupTableData[][] lookupTableData { get; set; }

  public class LookupTableData
  {
      public string Key { get; set; }
      public object Value { get; set; }
  }

  public LookupTableData[] FindById( int id )
  {
    if (this.lookupTableData == null) throw new InvalidOperationException();

    foreach ( LookupTableData[] entry in lookupTableData )
    {
      if (entry != null)
      {
        foreach( LookupTableData item in entry )
        {
          bool isMatch =  "id".Equals( item.Key ?? "", StringComparison.Ordinal )
                       && item.Value is int
                       && ((int)item.Value) == id
                       ;
          if ( isMatch )
          {
            return entry;
          }
        }
      }
    }
    return null;
  }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55698390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档