【发布时间】:2017-04-27 17:05:03
【问题描述】:
this.CurrentComponent.ExtendedProperties 是从 .Net 返回的 Dictionary<string, string>。当我尝试在客户端读取字典中的值时,在 javascript 中,我得到了该属性的所有对象,我不希望这样。
我有以下代码来填充对象并在 Javascript 中推送到数组。
this.populateExtendedProps = function () {
if (this.CurrentComponent.ExtendedProperties != null) {
$.each(this.CurrentComponent.ExtendedProperties, function (key, value) {
debugger;
var data = {};
data.Key = key;
data.Value = value;
data.Id = key + "_" + value; //generate
result.push(data);
});
}
};
this.CurrentComponent.ExtendedProperties 对象如下所示:
我想要的只是将键中的 FirstName 和值中的 FirstValue 返回给回调,但我也得到了event: object 等,这不是我想要的。
我怎样才能只返回字典中的实际值,而不是 javascript 对象的所有成员/属性/函数?
更新
这是我从服务器端返回的对象,.Net。从这里可以看出,ExtendedProperties 属性是Dictionary<string, string>
public class ComponentItem
{
public Guid ItemId { get; set; }
public Guid ComponentId { get; set; }
public Guid? ParentItemId { get; set; }
public string Name { get; set; }
public int Rank { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public string URL { get; set; }
public ComponentRendering OpenIn { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
public bool ActiveOnPreview { get; set; }
public bool ActiveOnLive { get; set; }
public Guid RepositoryId { get; set; }
public string RepositoryPath { get; set; }
public string CDNUrl { get; set; }
public bool CanDisableInLive { get; set; }
public bool CanDelete { get; set; }
public Dictionary<string, string> ExtendedProperties { get; set; }
}
这个ComponentItem 类被返回到前端以响应Ajax 调用。
这个ExtendedProperty 值是从数据库接收的,作为JSON 字符串,如下:{"FirstName":"FirstValue"}
我使用下面这段代码,将这个 JSON 字符串反序列化为 Dictionary,如下:
private Dictionary<string, string> GetExtendedPropertiesFromJson(string extendedPropertiesJson)
{
return JsonConvert.DeserializeObject<Dictionary<string, string>>(extendedPropertiesJson);
}
【问题讨论】:
-
你想在服务器端限制细节或者你想在执行循环之前在客户端检查对象的名字
-
我没有在 .NET 中工作过,所以我不确定服务器端到底发生了什么,但不幸的是他们没有将“真实”数据放入自己的属性中,因为很明显,当它到达 JS 时,它是一个具有属性的常规对象,而不是字典......您当然可以通过在推送之前将它们与键进行比较来将您期望的值列入白名单,但这显然是一种痛苦。
-
您只想要
FirstName属性,还是也想要uid? -
正如我在您的图片中看到的,this.CurrentComponent.ExtendedProperties 不是列表/数组。你确定这是一个数组吗?
-
@JericCruz 它是一个对象,但 $.each 也处理它:api.jquery.com/jquery.each
标签: javascript c# jquery asp.net .net