How to map data in JsonConvert
by I'm using Newtonsoft.Json and use loop foreach data to convert jsonString to jsonObject and I want to get data like the below result that I want to group from jsonObject all. Thank for help.
My parameter :
public class FundInPortRes
{
public string fcode { get; set; }
public string abbreviation { get; set; }
public string chnfname { get; set; }
public string AMC { get; set; }
public string chnmain_scope { get; set; }
public string risklevel { get; set; }
public List<FundInPortNAVRes> NAV { get; set; }
}
public class FundInPortNAVRes
{
public string fcode { get; set; }
public string navdate { get; set; }
public string nav { get; set; }
public double navDiff { get; set; }
}
My code :
Array Data F.NAV = [{"fcode":"004123"}, {"nav":"12.83210"}, {"navdate":"20190305"}, {"navDiff":"-0.0351"}]
[HttpPost]
public List<Models.FundInPortRes> GetFund2InPort(Models.FundInPortReq model)
{
DataAccess.DashDAL dal = new DataAccess.DashDAL();
List<Models.FundAllRes> models = dal.GetFund2InPort(model);
List<Models.FundInPortRes> result = new List<Models.FundInPortRes>();
if (models.Count > 0)
{
foreach (var F in models)
{
Models.FundInPortRes resData = new Models.FundInPortRes();
resData.fcode = F.fcode;
resData.abbreviation = F.abbreviation;
resData.chnfname = F.chnfname;
resData.AMC = F.AMC;
resData.chnmain_scope = F.chnmain_scope;
resData.risklevel = F.risklevel;
resData.NAV = JsonConvert.DeserializeObject<List<Models.FundInPortNAVRes>>(F.NAV);
result.Add(resData);
}
}
return result;
}
My result :
[{
"fcode": "004009",
"abbreviation": "FLEX-RMF",
"AMC": "ONEONLINE",
"chnmain_scope": "Fund All",
"risklevel": "5",
"NAV": [{
"fcode": "004009",
"navdate": null,
"nav": null,
"navDiff": 0.0
},
{
"fcode": null,
"navdate": null,
"nav": "27.14270",
"navDiff": 0.0
},
{
"fcode": null,
"navdate": "20190306",
"nav": null,
"navDiff": 0.0
},
{
"fcode": null,
"navdate": null,
"nav": null,
"navDiff": -0.1715
}
]
}]
Result that I want:
[{
"fcode": "004009",
"abbreviation": "FLEX-RMF",
"AMC": "ONEONLINE",
"chnmain_scope": "Fund All",
"risklevel": "5",
"NAV": [{
"fcode": "004009",
"navdate": "20190306",
"nav": "27.14270",
"navDiff": -0.1715
}
]
}]
Just use JsonProperty from the Newtonsoft. Here is a sample code.
using Newtonsoft.Json;
// ...
public class FundInPortRes
{
[JsonProperty("NameOfThePropertyToMap")]
public string fcode { get; set; }
// Other properties
}
Update:
In your case the result is correct. What you did was just create a list of 'FundInPortNAVRes', with the value of each field assigned to different objects. What you need to do in your case is just combine the value of all the fields into a single object. Here is a simple approach:
// Here every field are assigned to different object instead of one.
var json = "[{ \"fcode\":\"004123\"},{ \"nav\":\"12.83210\"},{ \"navdate\":\"20190305\"},{ \"nav2\":\"12.8672\"},{ \"navDate2\":\"20190306\"},{ \"navDiff\":\"-0.0351\"}]";
// so we change the json so that the field will be for a single object.
var actualRequiredJson = json.Replace("},{", ",");
// For multiple items but in a specific sequence
var multipleItemsJson = actualRequiredJson.Replace(",\"fcode\":","},{\"fcode\":");
var response = JsonConvert.DeserializeObject<List<FundInPortNAVRes>>(actualRequiredJson);