Reputation: 100
I have two classes. First one is:
public int id { get; set; }
public List<AnotherClass> field1 { get; set; }
And the second one is
public class AnotherClass
{
[JsonIgnore]
public int id { get; set; }
public string subfieldValue { get; set; }
}
when I make object from these classes and serialize them I get below JSON from them
{
"id" : "1",
"field1": [
{
"subfieldValue":"value1"
},
{
"subfieldValue":"value2"
}
]
}
But I need below JSON how can I do that? Also I can't use List<string>
for field1 type because I want to save this values to database with Entity Framework
{
"id" : "1",
"field1": ["value1", "value2"]
}
Upvotes: 0
Views: 123
Reputation: 13409
public int id { get; set; }
[JsonIgnore]
public List<AnotherClass> field1s { get; set; }
public List<string> field1
{
get{return field1s.Select(f=>f. subfieldValue).ToList();}
set{}
}
Also you probably should maintain the common C# naming convention like Id
instead of id
and change the json name through attribute with whatever you like in your json.
Upvotes: 3