我具有以下模型,我想从“属性”类中访问“名称和类型”:
public partial class RootObject
{
[JsonProperty("edmx:Edmx")]
public EdmxEdmx EdmxEdmx { get; set; }
}
public partial class EdmxEdmx
{
[JsonProperty("EntityType")]
public List<EntityType> EntityType { get; set; }
}
public partial class EntityType
{
[JsonProperty("-Name")]
public string Name { get; set; }
[JsonProperty("Property")]
public List<Property> Property { get; set; }
}
public partial class Property
{
[JsonProperty("-Name")]
public string Name { get; set; }
[JsonProperty("-Type")]
public String Type { get; set; }
}
我已经完成以下工作:
var r = JsonConvert.DeserializeObject<RootObject>(o1.ToString());
如何编写一个访问名称和类型的循环?
编辑: 这是我的JSON:
{
"edmx:Edmx": {
"-xmlns:edmx": "http://docs.oasis-open.org/odata/ns/edmx",
"-Version": "4.0",
"EntityType": [
{
"-BaseType": "mscrm.crmbaseentity",
"-Name": "EntityA",
"Property": [
{
"-Name": "address2_line1",
"-Unicode": "false",
"-Type": "Edm.String"
},
{
"-Name": "territorycode",
"-Type": "Edm.Int32"
},
{
"-Name": "EntityID",
"-Type": "Edm.Guid"
},
{
"-Name": "address1_telephone1",
"-Unicode": "false",
"-Type": "Edm.String"
}
]
}
}
}
上面的JSON是o1.ToString。我使用JObject从文本文件创建JSON对象。
EDIT2:
答案 0 :(得分:0)
我不确定是什么使您为此烦恼。您拥有了所有的组件,所需的只是一个嵌套的foreach循环。
library(data.table)
# function to recode values
myfun <- function(val){
if(is.na(val)) return (NA)
else switch(val, '1'= '0','2' = '1', '3'='2','4'='3')
}
# apply the function to the selected columns
col_names <- paste0('var', 1:59)
df[,(col_names) := lapply(.SD, function(x) unlist(sapply(x, myfun)) ), .SDcols = col_names]
print(df)