Reputation: 1
Here is my code
public class DetailRequest
{
public DetailRequest(string IDs, string Names, string Amounts)
{
REQDTL = new List<Detail>();
Detail detail = new Detail();
detail.ID = IDs;
detail.NAME = Names;
detail.AMOUNT = Amounts;
REQDTL.Add(detail);
}
public List<Detail> REQDTL { get; set; }
public class Detail
{
[Description("ID")]
public string ID { get; set; }
[Description("NAME")]
public string NAME { get; set; }
[Description("AMOUNT")]
public string AMOUNT { get; set; }
}
}
And
private static string _Serialize<T>(T entity)
{
return JsonConvert.SerializeObject(entity);
}
I'm getting the below structure after JSON convert
{
"REQDTL":[
{
"ID":"148488,148489,148486",
"NAME":"Test1,Test2,Test3",
"AMOUNT":"500,600,700"
}
]
}
But I want request as below structure
{
"REQDTL":[
{
"ID":"148488",
"NAME":"Test1",
"AMOUNT":"500"
},
{
"ID":"148489",
"NAME":"Test2",
"AMOUNT":"600"
},
{
"ID":"148486",
"NAME":"Test3",
"AMOUNT":"700"
}
]
}
Upvotes: 0
Views: 36
Reputation: 7054
I think you should modify DetailRequest
constructor:
public DetailRequest(string IDs, string Names, string Amounts)
{
REQDTL = IDs.Split(',').Zip(Names.Split(','), (id, name) => new {id, name})
.Zip(Amounts.Split(','), (x, amount) => new Detail
{
ID = x.id,
NAME = x.name,
AMOUNT = amount
}).ToList();
}
Usage
var entity = new DetailRequest("148488,148489,148486", "Test1,Test2,Test3", "500,600,700");
var res = JsonConvert.SerializeObject(entity);
Upvotes: 1